2023-09-27 12:27:46 +00:00
|
|
|
import { router, workspaceOwnerProcedure, workspaceProcedure } from '../trpc';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { prisma } from '../../model/_client';
|
2023-09-27 17:04:17 +00:00
|
|
|
import { sendNotification } from '../../model/notification';
|
2023-09-27 12:27:46 +00:00
|
|
|
|
|
|
|
export const notificationRouter = router({
|
2023-10-19 16:28:46 +00:00
|
|
|
all: workspaceProcedure.query(({ input }) => {
|
2023-09-27 12:27:46 +00:00
|
|
|
const workspaceId = input.workspaceId;
|
|
|
|
|
|
|
|
return prisma.notification.findMany({
|
|
|
|
where: {
|
|
|
|
workspaceId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}),
|
2023-09-27 17:04:17 +00:00
|
|
|
test: workspaceProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.string().optional(),
|
|
|
|
name: z.string(),
|
|
|
|
type: z.string(),
|
|
|
|
payload: z.object({}).passthrough(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ input }) => {
|
2023-10-19 16:28:46 +00:00
|
|
|
await sendNotification(
|
|
|
|
input,
|
|
|
|
`${input.name} Notification Testing`,
|
|
|
|
`This is Notification Testing`
|
|
|
|
);
|
2023-09-27 17:04:17 +00:00
|
|
|
}),
|
2023-09-27 12:27:46 +00:00
|
|
|
upsert: workspaceOwnerProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.string().optional(),
|
|
|
|
name: z.string(),
|
|
|
|
type: z.string(),
|
|
|
|
payload: z.any(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ input }) => {
|
|
|
|
const { workspaceId, id, name, type, payload } = input;
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
// update
|
|
|
|
return await prisma.notification.update({
|
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
payload,
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
workspaceId,
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// create
|
|
|
|
return await prisma.notification.create({
|
|
|
|
data: {
|
|
|
|
workspaceId,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
payload,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
2023-09-27 12:45:19 +00:00
|
|
|
delete: workspaceOwnerProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.string(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ input }) => {
|
|
|
|
const { id, workspaceId } = input;
|
|
|
|
|
|
|
|
return await prisma.notification.delete({
|
|
|
|
where: {
|
|
|
|
workspaceId,
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}),
|
2023-09-27 12:27:46 +00:00
|
|
|
});
|