mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
feat: support team webhooks
This commit is contained in:
@ -7,6 +7,7 @@ export interface CreateWebhookOptions {
|
||||
secret: string | null;
|
||||
enabled: boolean;
|
||||
userId: number;
|
||||
teamId?: number;
|
||||
}
|
||||
|
||||
export const createWebhook = async ({
|
||||
@ -15,7 +16,21 @@ export const createWebhook = async ({
|
||||
secret,
|
||||
enabled,
|
||||
userId,
|
||||
teamId,
|
||||
}: CreateWebhookOptions) => {
|
||||
if (teamId) {
|
||||
await prisma.team.findFirstOrThrow({
|
||||
where: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return await prisma.webhook.create({
|
||||
data: {
|
||||
webhookUrl,
|
||||
@ -23,6 +38,7 @@ export const createWebhook = async ({
|
||||
secret,
|
||||
enabled,
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -3,13 +3,28 @@ import { prisma } from '@documenso/prisma';
|
||||
export type DeleteWebhookByIdOptions = {
|
||||
id: string;
|
||||
userId: number;
|
||||
teamId?: number;
|
||||
};
|
||||
|
||||
export const deleteWebhookById = async ({ id, userId }: DeleteWebhookByIdOptions) => {
|
||||
export const deleteWebhookById = async ({ id, userId, teamId }: DeleteWebhookByIdOptions) => {
|
||||
return await prisma.webhook.delete({
|
||||
where: {
|
||||
id,
|
||||
userId,
|
||||
...(teamId
|
||||
? {
|
||||
team: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: {
|
||||
userId,
|
||||
teamId: null,
|
||||
}),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -4,15 +4,30 @@ import { prisma } from '@documenso/prisma';
|
||||
|
||||
export type EditWebhookOptions = {
|
||||
id: string;
|
||||
data: Prisma.WebhookUpdateInput;
|
||||
data: Omit<Prisma.WebhookUpdateInput, 'id' | 'userId' | 'teamId'>;
|
||||
userId: number;
|
||||
teamId?: number;
|
||||
};
|
||||
|
||||
export const editWebhook = async ({ id, data, userId }: EditWebhookOptions) => {
|
||||
export const editWebhook = async ({ id, data, userId, teamId }: EditWebhookOptions) => {
|
||||
return await prisma.webhook.update({
|
||||
where: {
|
||||
id,
|
||||
userId,
|
||||
...(teamId
|
||||
? {
|
||||
team: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: {
|
||||
userId,
|
||||
teamId: null,
|
||||
}),
|
||||
},
|
||||
data: {
|
||||
...data,
|
||||
|
||||
@ -14,6 +14,10 @@ export const getAllWebhooksByEventTrigger = async ({
|
||||
}: GetAllWebhooksByEventTriggerOptions) => {
|
||||
return prisma.webhook.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
eventTriggers: {
|
||||
has: event,
|
||||
},
|
||||
...(teamId
|
||||
? {
|
||||
team: {
|
||||
@ -29,10 +33,6 @@ export const getAllWebhooksByEventTrigger = async ({
|
||||
userId,
|
||||
teamId: null,
|
||||
}),
|
||||
eventTriggers: {
|
||||
has: event,
|
||||
},
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@ -3,13 +3,28 @@ import { prisma } from '@documenso/prisma';
|
||||
export type GetWebhookByIdOptions = {
|
||||
id: string;
|
||||
userId: number;
|
||||
teamId?: number;
|
||||
};
|
||||
|
||||
export const getWebhookById = async ({ id, userId }: GetWebhookByIdOptions) => {
|
||||
export const getWebhookById = async ({ id, userId, teamId }: GetWebhookByIdOptions) => {
|
||||
return await prisma.webhook.findFirstOrThrow({
|
||||
where: {
|
||||
id,
|
||||
userId,
|
||||
...(teamId
|
||||
? {
|
||||
team: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: {
|
||||
userId,
|
||||
teamId: null,
|
||||
}),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
19
packages/lib/server-only/webhooks/get-webhooks-by-team-id.ts
Normal file
19
packages/lib/server-only/webhooks/get-webhooks-by-team-id.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
export const getWebhooksByTeamId = async (teamId: number, userId: number) => {
|
||||
return await prisma.webhook.findMany({
|
||||
where: {
|
||||
team: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user