Skip to content

Commit ec90836

Browse files
committed
Lint + moved notifcation type
1 parent 64d43fd commit ec90836

5 files changed

Lines changed: 72 additions & 45 deletions

File tree

apps/rpc/src/modules/notification/notification-repository.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1-
import { DBHandle } from "@dotkomonline/db"
2-
import { Notification, NotificationId, NotificationWrite, UserId, NotificationRecipientId, NotificationRecipient } from "@dotkomonline/types"
1+
import type { DBHandle } from "@dotkomonline/db"
2+
import type {
3+
Notification,
4+
NotificationId,
5+
NotificationWrite,
6+
NotificationRecipientId,
7+
NotificationRecipient,
8+
UserNotification,
9+
} from "./notification"
310

4-
export interface NotificationRepository {
11+
import type { UserId } from "@dotkomonline/types"
512

13+
export interface NotificationRepository {
614
findById(handle: DBHandle, notificationId: NotificationId): Promise<Notification | null>
715
create(handle: DBHandle, notificationData: NotificationWrite): Promise<Notification>
8-
update(handle: DBHandle, notificationId: NotificationId, notificationData: Partial<NotificationWrite>): Promise<Notification>
16+
update(
17+
handle: DBHandle,
18+
notificationId: NotificationId,
19+
notificationData: Partial<NotificationWrite>
20+
): Promise<Notification>
921
delete(handle: DBHandle, notificationId: NotificationId): Promise<Notification | null>
1022

1123
addRecipients(handle: DBHandle, notificationId: NotificationId, recipientIds: UserId[]): Promise<void>
1224
removeRecipients(handle: DBHandle, notificationId: NotificationId, recipientIds: UserId[]): Promise<void>
1325

14-
findRecipient(handle: DBHandle, recipientId: NotificationRecipientId, userId: UserId): Promise<NotificationRecipient | null>
15-
findAllforUser(handle: DBHandle, userId: UserId): Promise<Notification[]>
16-
getUnreadCountforUser(handle: DBHandle, userId: UserId): Promise<number>
26+
findRecipient(
27+
handle: DBHandle,
28+
recipientId: NotificationRecipientId,
29+
userId: UserId
30+
): Promise<NotificationRecipient | null>
31+
findAllForUser(handle: DBHandle, userId: UserId): Promise<UserNotification[]>
32+
getUnreadCountForUser(handle: DBHandle, userId: UserId): Promise<number>
1733
markAsRead(handle: DBHandle, notificationId: NotificationId, userId: UserId): Promise<void>
1834
markAllAsRead(handle: DBHandle, userId: UserId): Promise<void>
1935
}
@@ -36,10 +52,9 @@ export function getNotificationRepository(): NotificationRepository {
3652
const { recipientIds, ...data } = notificationData
3753
const notification = await handle.notification.update({
3854
where: { id: notificationId },
39-
data,
55+
data,
4056
})
4157
return notification
42-
4358
},
4459
async delete(handle, notificationId) {
4560
const notification = await handle.notification.findUnique({
@@ -68,12 +83,12 @@ export function getNotificationRepository(): NotificationRepository {
6883
})
6984
},
7085
async removeRecipients(handle, notificationId, recipientIds) {
71-
await handle.notificationRecipient.deleteMany({
72-
where: {
73-
notificationId,
74-
userId: { in: recipientIds },
75-
},
76-
})
86+
await handle.notificationRecipient.deleteMany({
87+
where: {
88+
notificationId,
89+
userId: { in: recipientIds },
90+
},
91+
})
7792
},
7893

7994
async findAllForUser(handle, userId) {
@@ -83,17 +98,17 @@ export function getNotificationRepository(): NotificationRepository {
8398
})
8499
},
85100

86-
async getUnreadCountforUser(handle, userId) {
87-
await handle.notificationRecipient.count({
88-
where: { userId, readAt: null },
89-
})
90-
},
91-
92101
async getUnreadCountForUser(handle, userId) {
93102
return handle.notificationRecipient.count({
94103
where: { userId, readAt: null },
95104
})
96105
},
106+
async markAsRead(handle, notificationId, userId) {
107+
await handle.notificationRecipient.updateMany({
108+
where: { notificationId, userId, readAt: null },
109+
data: { readAt: new Date() },
110+
})
111+
},
97112

98113
async markAllAsRead(handle, userId) {
99114
await handle.notificationRecipient.updateMany({
@@ -102,4 +117,4 @@ export function getNotificationRepository(): NotificationRepository {
102117
})
103118
},
104119
}
105-
}
120+
}

apps/rpc/src/modules/notification/notification-router.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { NotificationSchema, NotificationWriteSchema, UserNotificationSchema } from "@dotkomonline/types"
21
import type { inferProcedureInput, inferProcedureOutput } from "@trpc/server"
32
import { z } from "zod"
43
import { isEditor } from "../../authorization"
54
import { withAuditLogEntry, withAuthentication, withAuthorization, withDatabaseTransaction } from "../../middlewares"
65
import { procedure, t } from "../../trpc"
6+
import { NotificationSchema, NotificationWriteSchema } from "./notification"
77

88
export type GetNotificationInput = inferProcedureInput<typeof getNotificationProcedure>
99
export type GetNotificationOutput = inferProcedureOutput<typeof getNotificationProcedure>
@@ -29,10 +29,12 @@ const createNotificationProcedure = procedure
2929
export type EditNotificationInput = inferProcedureInput<typeof editNotificationProcedure>
3030
export type EditNotificationOutput = inferProcedureOutput<typeof editNotificationProcedure>
3131
const editNotificationProcedure = procedure
32-
.input(z.object({
33-
id: NotificationSchema.shape.id,
34-
input: NotificationWriteSchema.partial(),
35-
}))
32+
.input(
33+
z.object({
34+
id: NotificationSchema.shape.id,
35+
input: NotificationWriteSchema.partial(),
36+
})
37+
)
3638
.use(withAuthentication())
3739
.use(withAuthorization(isEditor()))
3840
.use(withDatabaseTransaction())
@@ -99,4 +101,4 @@ export const notificationRouter = t.router({
99101
getUnreadCount: getUnreadCountProcedure,
100102
markAsRead: markAsReadProcedure,
101103
markAllAsRead: markAllAsReadProcedure,
102-
})
104+
})

apps/rpc/src/modules/notification/notification-service.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import type { DBHandle } from "@dotkomonline/db"
2-
import { NotFoundError } from "../../error"
3-
import type { Notification, NotificationId } from "@dotkomonline/types"
4-
import { NotificationRepository } from "./notification-repository"
2+
import type {
3+
Notification,
4+
NotificationId,
5+
NotificationRecipient,
6+
NotificationRecipientId,
7+
NotificationWrite,
8+
UserNotification,
9+
} from "./notification"
10+
import type { UserId } from "@dotkomonline/types"
11+
import type { NotificationRepository } from "./notification-repository"
512

613
export interface NotificationService {
714
findById(handle: DBHandle, notificationId: NotificationId): Promise<Notification | null>
815
create(handle: DBHandle, notificationData: NotificationWrite): Promise<Notification>
9-
update(handle: DBHandle, notificationId: NotificationId, notificationData: Partial<NotificationWrite>): Promise<Notification>
16+
update(
17+
handle: DBHandle,
18+
notificationId: NotificationId,
19+
notificationData: Partial<NotificationWrite>
20+
): Promise<Notification>
1021
delete(handle: DBHandle, notificationId: NotificationId): Promise<Notification | null>
1122

1223
addRecipients(handle: DBHandle, notificationId: NotificationId, recipientIds: UserId[]): Promise<void>
1324
removeRecipients(handle: DBHandle, notificationId: NotificationId, recipientIds: UserId[]): Promise<void>
1425

15-
findRecipient(handle: DBHandle, recipientId: NotificationRecipientId, userId: UserId): Promise<NotificationRecipient | null>
16-
findAllforUser(handle: DBHandle, userId: UserId): Promise<Notification[]>
17-
getUnreadCountforUser(handle: DBHandle, userId: UserId): Promise<number>
26+
findRecipient(
27+
handle: DBHandle,
28+
recipientId: NotificationRecipientId,
29+
userId: UserId
30+
): Promise<NotificationRecipient | null>
31+
findAllForUser(handle: DBHandle, userId: UserId): Promise<UserNotification[]>
32+
getUnreadCountForUser(handle: DBHandle, userId: UserId): Promise<number>
1833
markAsRead(handle: DBHandle, notificationId: NotificationId, userId: UserId): Promise<void>
1934
markAllAsRead(handle: DBHandle, userId: UserId): Promise<void>
2035
}
@@ -29,7 +44,6 @@ export function getNotificationService(notificationRepository: NotificationRepos
2944
},
3045

3146
async update(handle, notificationId, notificationData) {
32-
const notification = await notificationRepository.findById(handle, notificationId)
3347
return await notificationRepository.update(handle, notificationId, notificationData)
3448
},
3549

@@ -49,12 +63,12 @@ export function getNotificationService(notificationRepository: NotificationRepos
4963
return await notificationRepository.findRecipient(handle, recipientId, userId)
5064
},
5165

52-
async findAllforUser(handle, userId) {
53-
return await notificationRepository.findAllforUser(handle, userId)
66+
async findAllForUser(handle, userId) {
67+
return await notificationRepository.findAllForUser(handle, userId)
5468
},
5569

56-
async getUnreadCountforUser(handle, userId) {
57-
return await notificationRepository.getUnreadCountforUser(handle, userId)
70+
async getUnreadCountForUser(handle, userId) {
71+
return await notificationRepository.getUnreadCountForUser(handle, userId)
5872
},
5973

6074
async markAsRead(handle, notificationId, userId) {
@@ -63,6 +77,6 @@ export function getNotificationService(notificationRepository: NotificationRepos
6377

6478
async markAllAsRead(handle, userId) {
6579
await notificationRepository.markAllAsRead(handle, userId)
66-
}
80+
},
6781
}
6882
}

packages/types/src/notification.ts renamed to apps/rpc/src/modules/notification/notification.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export type NotificationRecipient = z.infer<typeof NotificationRecipientSchema>
1515

1616
export type NotificationRecipientId = NotificationRecipient["id"]
1717

18-
1918
export type UserNotification = z.infer<typeof UserNotificationSchema>
2019
export const UserNotificationSchema = schemas.NotificationRecipientSchema.extend({
2120
notification: schemas.NotificationSchema,
@@ -34,5 +33,3 @@ export const NotificationWriteSchema = NotificationSchema.pick({
3433
}).extend({
3534
recipientIds: z.array(z.string().uuid()).min(1),
3635
})
37-
38-

packages/types/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ export * from "./task"
1414
export * from "./user"
1515
export * from "./audit-log"
1616
export * from "./workspace-sync"
17-
export * from "./notification"

0 commit comments

Comments
 (0)