Skip to content

Commit

Permalink
Noti
Browse files Browse the repository at this point in the history
  • Loading branch information
HUNG-rushb committed Dec 1, 2023
1 parent 5b233ae commit 4051401
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 80 deletions.
38 changes: 38 additions & 0 deletions src/Type_Definitions/Noti_Noti.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import gql from 'graphql-tag';

const notiDefs = gql`
extend type Query {
allNotis: [Notification]!
userNotis(data: UserNotiInfoInput!): [Notification]!
}
input UserNotiInfoInput {
userId: ID!
}
# _______________________________________________________
# _______________________________________________________
extend type Mutation {
deleteNoti(data: DeleteNotiInput!): Notification!
deleteAllNoti: DeleteAllReturnType!
}
input DeleteNotiInput {
notiId: ID!
}
type Notification {
id: ID!
type: String!
postId: String!
userTriggerId: User!
userIds: [User]!
createdAt: String!
}
`;

export default notiDefs;
1 change: 1 addition & 0 deletions src/Type_Definitions/User_User.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const userDefs = gql`
level: Level!
posts: [Post]!
notiIds: [Notification]!
stories: [Story]!
albums: [Album]!
Expand Down
2 changes: 2 additions & 0 deletions src/Type_Definitions/_typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import contestDefs from './Contest_Contest.js';
import chatDefs from './Chat_Chat.js';
import messageDefs from './Message_Message.js';
import skillDefs from './Skill_Skill.js';
import notiDefs from './Noti_Noti.js';

const baseDefs = gql`
type Query {
Expand Down Expand Up @@ -55,6 +56,7 @@ const typeDefs = [
chatDefs,
messageDefs,
skillDefs,
notiDefs,
];

export default typeDefs;
4 changes: 2 additions & 2 deletions src/notify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// https://firebase.google.com/docs/cloud-messaging/send-message
import { firebaseMessaging } from './index.js';

export const sendNotificationToClient = (tokens, data, notification) => {
export const sendNotificationToClient = (tokens, data) => {
// Send a message to the devices corresponding to the provided
// registration tokens.
firebaseMessaging
.sendMulticast({ tokens, data, notification })
.sendMulticast({ tokens, data })
.then((response) => {
// Response is an object of the form { responses: [] }
const successes = response.responses.filter(
Expand Down
2 changes: 1 addition & 1 deletion src/prisma/ERD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 34 additions & 22 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ model User {
messages Message[]
// courses Course[]
// notifications Notification[]
notiIds String[] @db.ObjectId
user_to_notification Notification[] @relation( fields: [notiIds], references: [id])
notificationsTriggered Notification[] @relation(name: "noti_trigger")
chatIDs String[] @db.ObjectId
user_to_chat Chat[] @relation(fields: [chatIDs], references: [id])
courseIDs String[] @db.ObjectId
user_to_course Course[] @relation(fields: [courseIDs], references: [id])
// courseIDs String[] @db.ObjectId
// user_to_course Course[] @relation(fields: [courseIDs], references: [id])
endoseredIds String[] @db.ObjectId
endoseredList Endorsement[] @relation(name: "endorser", fields: [endoseredIds], references: [id])
Expand All @@ -67,14 +70,23 @@ model User {
}

// model Notification {
// id String @id @default(auto()) @map("_id") @db.ObjectId
// content String
// isRead Boolean
enum TypeNoti {
POST_CREATED
POST_LIKED
}

// userId String @db.ObjectId
// notification_to_user User @relation(fields: [userId], references: [id])
// }
model Notification {
id String @id @default(auto()) @map("_id") @db.ObjectId
type TypeNoti
postId String
createdAt DateTime @default(now())
userIds String[] @db.ObjectId
notification_to_user User[] @relation( fields: [userIds], references: [id])
userTriggerId String @db.ObjectId
notification_to_usertrigger User @relation(name: "noti_trigger",fields: [userTriggerId], references: [id])
}

model Endorsement {
id String @id @default(auto()) @map("_id") @db.ObjectId
Expand Down Expand Up @@ -297,21 +309,21 @@ model Contest {
updatedAt DateTime @updatedAt
}

model Course {
id String @id @default(auto()) @map("_id") @db.ObjectId
lessons Lesson[]
// model Course {
// id String @id @default(auto()) @map("_id") @db.ObjectId
// lessons Lesson[]

userIDs String[] @db.ObjectId
course_to_user User[] @relation(fields: [userIDs], references: [id])
}
// userIDs String[] @db.ObjectId
// course_to_user User[] @relation(fields: [userIDs], references: [id])
// }

model Lesson {
id String @id @default(auto()) @map("_id") @db.ObjectId
content String
// model Lesson {
// id String @id @default(auto()) @map("_id") @db.ObjectId
// content String

courseId String @db.ObjectId
lesson_to_course Course @relation(fields: [courseId], references: [id])
}
// courseId String @db.ObjectId
// lesson_to_course Course @relation(fields: [courseId], references: [id])
// }

// Chat Application
model Chat {
Expand Down
18 changes: 2 additions & 16 deletions src/resolvers/Mutation/_Mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,29 @@ import messageMutation from './message.js';
import profileMutation from './profile.js';
import skillMutation from './skill.js';
import utilityMutation from './utility.js';
import notiMutation from './notification.js';

const Mutation = {
// User
...userMutation,
// Level
...levelMutation,
// Image
...imageMutation,
// Image Info
...imageInfoMutation,
// Post
...postMutation,
// Story
...storyMutation,
// Comment
...commentMutation,
// Category
...categoryMutation,
// Album
...albumMutation,
// Tag
...tagMutation,
// Follower
...followerMutation,
// Following
...followingMutation,
// Report
...reportMutation,
// Contest
...contestMutation,
// Chat
...chatMutation,
// Message
...messageMutation,
...profileMutation,
...skillMutation,
...utilityMutation,
...notiMutation,
};

export default Mutation;
32 changes: 32 additions & 0 deletions src/resolvers/Mutation/notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { prisma } from '../../prisma/database.js';

const notiMutation = {
deleteNoti: async (parent, args, info) => {
let notification;
try {
notification = await prisma.notification.delete({
where: {
id: args.data.notiId,
},
});
} catch (e) {
console.log(e);
throw e;
}

return notification;
},
deleteAllNoti: async (parent, args, info) => {
let result;
try {
result = await prisma.notification.deleteMany({});
} catch (e) {
console.log(e);
throw e;
}

return result;
},
};

export default notiMutation;
27 changes: 20 additions & 7 deletions src/resolvers/Mutation/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ const postMutation = {
let post;
const imageHash = await hashImage(args.data.imageURL);
try {
const follower = await prisma.follower.findUnique({
where: {
userId: args.data.userId,
},
});
console.log({ follower });

post = await prisma.post.create({
data: {
title: args.data.title,
Expand Down Expand Up @@ -42,19 +49,25 @@ const postMutation = {
},
},
});
console.log({ post });

const a = await prisma.notification.create({
data: {
type: 'POST_CREATED',
postId: post.id,
userTriggerId: post.userId,
userIds: follower.userFollower,
},
});
console.log({ a });

sendNotificationToClient(
[
'eUW71E0j4VAwZdHuyjdnQd:APA91bFKKXAsu_RxExCsDDK7V0AaqvHF9tW51bUBBDUkbvtxHEe9DpnFMhUfvgwVSAoud89y1rHxpeeEesWZZ9hkqAkkEMoP-7ys6QjYekcLln-bnXvvWfdG2ISZGwLtIm0iVH526VLr',
],
{
title: 'New post',
body: 'New post',
},
{
title: 'New post',
body: 'New post',
post,
title: 'Notify new post',
body: JSON.stringify({ post, follower }),
},
);
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions src/resolvers/Mutation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const userMutation = {
phoneNumber: '',
isAdmin: 0,
age: 18,
notifications: [],
level: {
create: {
currentXP: 0,
Expand Down
22 changes: 22 additions & 0 deletions src/resolvers/Query/NOTIFICATION.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { prisma } from '../../prisma/database.js';

const notiQuery = {
allNotis: async (parent, args, info) => {
return await prisma.notification.findMany();
},
userNotis: async (parent, args, info) => {
return await prisma.notification.findMany({
where: {
userIds: { has: args.data.userId },
},
orderBy: [
{
createdAt: 'desc',
},
// { isRead: 'desc' },
],
});
},
};

export default notiQuery;
Loading

0 comments on commit 4051401

Please sign in to comment.