1+ import { NotificationSchema , NotificationWriteSchema , UserNotificationSchema } from "@dotkomonline/types"
2+ import type { inferProcedureInput , inferProcedureOutput } from "@trpc/server"
3+ import { z } from "zod"
4+ import { isEditor } from "../../authorization"
5+ import { withAuditLogEntry , withAuthentication , withAuthorization , withDatabaseTransaction } from "../../middlewares"
6+ import { procedure , t } from "../../trpc"
7+
8+ export type GetNotificationInput = inferProcedureInput < typeof getNotificationProcedure >
9+ export type GetNotificationOutput = inferProcedureOutput < typeof getNotificationProcedure >
10+ const getNotificationProcedure = procedure
11+ . input ( NotificationSchema . shape . id )
12+ . use ( withDatabaseTransaction ( ) )
13+ . query ( async ( { input, ctx } ) => {
14+ return ctx . notificationService . findById ( ctx . handle , input )
15+ } )
16+
17+ export type CreateNotificationInput = inferProcedureInput < typeof createNotificationProcedure >
18+ export type CreateNotificationOutput = inferProcedureOutput < typeof createNotificationProcedure >
19+ const createNotificationProcedure = procedure
20+ . input ( NotificationWriteSchema )
21+ . use ( withAuthentication ( ) )
22+ . use ( withAuthorization ( isEditor ( ) ) )
23+ . use ( withDatabaseTransaction ( ) )
24+ . use ( withAuditLogEntry ( ) )
25+ . mutation ( async ( { input, ctx } ) => {
26+ return ctx . notificationService . create ( ctx . handle , input )
27+ } )
28+
29+ export type EditNotificationInput = inferProcedureInput < typeof editNotificationProcedure >
30+ export type EditNotificationOutput = inferProcedureOutput < typeof editNotificationProcedure >
31+ const editNotificationProcedure = procedure
32+ . input ( z . object ( {
33+ id : NotificationSchema . shape . id ,
34+ input : NotificationWriteSchema . partial ( ) ,
35+ } ) )
36+ . use ( withAuthentication ( ) )
37+ . use ( withAuthorization ( isEditor ( ) ) )
38+ . use ( withDatabaseTransaction ( ) )
39+ . use ( withAuditLogEntry ( ) )
40+ . mutation ( async ( { input : changes , ctx } ) => {
41+ return ctx . notificationService . update ( ctx . handle , changes . id , changes . input )
42+ } )
43+
44+ export type DeleteNotificationInput = inferProcedureInput < typeof deleteNotificationProcedure >
45+ export type DeleteNotificationOutput = inferProcedureOutput < typeof deleteNotificationProcedure >
46+ const deleteNotificationProcedure = procedure
47+ . input ( NotificationSchema . shape . id )
48+ . use ( withAuthentication ( ) )
49+ . use ( withAuthorization ( isEditor ( ) ) )
50+ . use ( withDatabaseTransaction ( ) )
51+ . use ( withAuditLogEntry ( ) )
52+ . mutation ( async ( { input, ctx } ) => {
53+ return ctx . notificationService . delete ( ctx . handle , input )
54+ } )
55+
56+ export type GetMyNotificationsInput = inferProcedureInput < typeof getMyNotificationsProcedure >
57+ export type GetMyNotificationsOutput = inferProcedureOutput < typeof getMyNotificationsProcedure >
58+ const getMyNotificationsProcedure = procedure
59+ . use ( withAuthentication ( ) )
60+ . use ( withDatabaseTransaction ( ) )
61+ . query ( async ( { ctx } ) => {
62+ return ctx . notificationService . findAllForUser ( ctx . handle , ctx . principal . subject )
63+ } )
64+
65+ export type GetUnreadCountInput = inferProcedureInput < typeof getUnreadCountProcedure >
66+ export type GetUnreadCountOutput = inferProcedureOutput < typeof getUnreadCountProcedure >
67+ const getUnreadCountProcedure = procedure
68+ . use ( withAuthentication ( ) )
69+ . use ( withDatabaseTransaction ( ) )
70+ . query ( async ( { ctx } ) => {
71+ return ctx . notificationService . getUnreadCountForUser ( ctx . handle , ctx . principal . subject )
72+ } )
73+
74+ export type MarkAsReadInput = inferProcedureInput < typeof markAsReadProcedure >
75+ export type MarkAsReadOutput = inferProcedureOutput < typeof markAsReadProcedure >
76+ const markAsReadProcedure = procedure
77+ . input ( z . object ( { notificationId : NotificationSchema . shape . id } ) )
78+ . use ( withAuthentication ( ) )
79+ . use ( withDatabaseTransaction ( ) )
80+ . mutation ( async ( { input, ctx } ) => {
81+ return ctx . notificationService . markAsRead ( ctx . handle , input . notificationId , ctx . principal . subject )
82+ } )
83+
84+ export type MarkAllAsReadInput = inferProcedureInput < typeof markAllAsReadProcedure >
85+ export type MarkAllAsReadOutput = inferProcedureOutput < typeof markAllAsReadProcedure >
86+ const markAllAsReadProcedure = procedure
87+ . use ( withAuthentication ( ) )
88+ . use ( withDatabaseTransaction ( ) )
89+ . mutation ( async ( { ctx } ) => {
90+ return ctx . notificationService . markAllAsRead ( ctx . handle , ctx . principal . subject )
91+ } )
92+
93+ export const notificationRouter = t . router ( {
94+ get : getNotificationProcedure ,
95+ create : createNotificationProcedure ,
96+ edit : editNotificationProcedure ,
97+ delete : deleteNotificationProcedure ,
98+ getMyNotifications : getMyNotificationsProcedure ,
99+ getUnreadCount : getUnreadCountProcedure ,
100+ markAsRead : markAsReadProcedure ,
101+ markAllAsRead : markAllAsReadProcedure ,
102+ } )
0 commit comments