Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Aplicativo “Eu luto”
backend
Commits
820f4259
Commit
820f4259
authored
1 year ago
by
Ramiro Nascimento
Browse files
Options
Download
Email Patches
Plain Diff
🗃
add event to notification
parent
cba4ded5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
prisma/migrations/20231117222549_notification_type_and_event_relation/migration.sql
+5
-0
...222549_notification_type_and_event_relation/migration.sql
prisma/schema.prisma
+15
-12
prisma/schema.prisma
src/repository/notifications/notificationRepository.ts
+22
-4
src/repository/notifications/notificationRepository.ts
src/services/notifications/notificationsService.ts
+2
-2
src/services/notifications/notificationsService.ts
with
44 additions
and
18 deletions
+44
-18
prisma/migrations/20231117222549_notification_type_and_event_relation/migration.sql
0 → 100644
View file @
820f4259
-- AlterTable
ALTER
TABLE
"notifications"
ADD
COLUMN
"eventId"
INTEGER
;
-- AddForeignKey
ALTER
TABLE
"notifications"
ADD
CONSTRAINT
"notifications_eventId_fkey"
FOREIGN
KEY
(
"eventId"
)
REFERENCES
"events"
(
"id"
)
ON
DELETE
SET
NULL
ON
UPDATE
CASCADE
;
This diff is collapsed.
Click to expand it.
prisma/schema.prisma
View file @
820f4259
...
...
@@ -8,7 +8,7 @@ datasource db {
}
model
Event
{
id
Int
@
id
@
default
(
autoincrement
())
id
Int
@
id
@
default
(
autoincrement
())
authorId
Int
patientId
Int
?
eventType
EventType
?
...
...
@@ -18,22 +18,25 @@ model Event {
endTime
String
status
EventStatus
?
active
Boolean
updatedAt
DateTime
?
@
updatedAt
author
User
@
relation
(
"authorEvent"
,
fields
:
[
authorId
],
references
:
[
id
])
patient
User
?
@
relation
(
"patientEvent"
,
fields
:
[
patientId
],
references
:
[
id
])
updatedAt
DateTime
?
@
updatedAt
author
User
@
relation
(
"authorEvent"
,
fields
:
[
authorId
],
references
:
[
id
])
patient
User
?
@
relation
(
"patientEvent"
,
fields
:
[
patientId
],
references
:
[
id
])
notifications
Notification
[]
@@
map
(
"events"
)
}
model
Notification
{
id
Int
@
id
@
default
(
autoincrement
())
type
NotificationType
?
userId
Int
?
title
String
?
message
String
?
createdAt
DateTime
@
default
(
now
())
sentAt
DateTime
?
user
User
?
@
relation
(
fields
:
[
userId
],
references
:
[
id
])
id
Int
@
id
@
default
(
autoincrement
())
type
NotificationType
?
eventId
Int
?
userId
Int
?
title
String
?
message
String
?
createdAt
DateTime
@
default
(
now
())
sentAt
DateTime
?
user
User
?
@
relation
(
fields
:
[
userId
],
references
:
[
id
])
event
Event
?
@
relation
(
fields
:
[
eventId
],
references
:
[
id
])
@@
map
(
"notifications"
)
}
...
...
This diff is collapsed.
Click to expand it.
src/repository/notifications/notificationRepository.ts
View file @
820f4259
import
{
NotificationType
,
Prisma
,
PrismaClient
}
from
"
@prisma/client
"
;
import
{
NotificationType
,
PrismaClient
}
from
"
@prisma/client
"
;
const
prisma
=
new
PrismaClient
();
...
...
@@ -9,18 +9,36 @@ export const getNotificationsByUserId = async (userId) => {
},
select
:
{
id
:
true
,
user
:
true
,
type
:
true
,
title
:
true
,
message
:
true
,
createdAt
:
true
,
sentAt
:
true
,
event
:
{
select
:
{
id
:
true
,
author
:
true
,
startTime
:
true
,
date
:
true
,
},
},
},
});
};
export
const
createNotification
=
async
(
data
:
{
userId
:
number
;
type
:
NotificationType
;
title
:
string
;
message
:
string
;
sentAt
:
Date
;
})
=>
{
const
notification
=
prisma
.
notification
.
create
({
type
Event
=
""
;
export
const
createNotification
=
async
(
data
:
{
userId
:
number
;
eventId
:
number
;
type
:
NotificationType
;
title
:
string
;
message
:
string
;
sentAt
:
Date
;
})
=>
{
const
notification
=
await
prisma
.
notification
.
create
({
data
:
{
eventId
:
data
.
eventId
,
userId
:
data
.
userId
,
type
:
data
.
type
,
title
:
data
.
title
,
...
...
This diff is collapsed.
Click to expand it.
src/services/notifications/notificationsService.ts
View file @
820f4259
...
...
@@ -12,7 +12,6 @@ export const getNotifications = (userId) => {
};
export
async
function
pushNotification
(
event
)
{
logger
.
info
(
"
-> Pushing notification for event
"
+
event
.
id
);
pushNotificationToPatient
(
event
);
...
...
@@ -62,6 +61,7 @@ function pushNotificationToPatient(event: any) {
createNotification
({
userId
:
event
.
patient
.
id
,
eventId
:
event
.
id
,
message
:
message
,
sentAt
:
new
Date
(),
title
:
title
,
...
...
@@ -77,7 +77,6 @@ function pushNotificationToPatient(event: any) {
message
:
message
,
});
logger
.
info
(
"
* Notification sent for patient
"
+
event
.
patient
.
username
);
}
function
pushNotificationToAuthor
(
event
:
any
)
{
...
...
@@ -114,6 +113,7 @@ function pushNotificationToAuthor(event: any) {
createNotification
({
userId
:
event
.
patient
.
id
,
eventId
:
event
.
id
,
message
:
message
,
sentAt
:
new
Date
(),
title
:
title
,
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help