Skip to content

Commit d7764fa

Browse files
committed
feat: send tests
1 parent d574486 commit d7764fa

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

apps/backend/src/api/routes/webhooks.controller.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
1+
import {
2+
Body,
3+
Controller,
4+
Delete,
5+
Get,
6+
Param,
7+
Post,
8+
Put,
9+
Query,
10+
} from '@nestjs/common';
211
import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';
312
import { Organization } from '@prisma/client';
413
import { ApiTags } from '@nestjs/swagger';
@@ -47,4 +56,19 @@ export class WebhookController {
4756
) {
4857
return this._webhooksService.deleteWebhook(org.id, id);
4958
}
59+
60+
@Post('/send')
61+
async sendWebhook(@Body() body: any, @Query('url') url: string) {
62+
try {
63+
await fetch(url, {
64+
method: 'POST',
65+
body: JSON.stringify(body),
66+
headers: { 'Content-Type': 'application/json' },
67+
});
68+
} catch (err) {
69+
/** sent **/
70+
}
71+
72+
return { send: true };
73+
}
5074
}

apps/frontend/src/components/webhooks/webhooks.tsx

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,51 @@ export const AddOrEditWebhook: FC<{ data?: any; reload: () => void }> = (
175175
[data, integrations]
176176
);
177177

178+
const sendTest = useCallback(async () => {
179+
const url = form.getValues('url');
180+
toast.show('Webhook send', 'success');
181+
try {
182+
await fetch(`/webhooks/send?url=${encodeURIComponent(url)}`, {
183+
method: 'POST',
184+
headers: {
185+
contentType: 'application/json',
186+
},
187+
body: JSON.stringify([
188+
{
189+
id: 'cm6tcts4f0005qcwit25cis26',
190+
content: 'This is the first post to instagram',
191+
publishDate: '2025-02-06T13:09:00.000Z',
192+
releaseURL: 'https://facebook.com/release/release',
193+
state: 'PUBLISHED',
194+
integration: {
195+
id: 'cm6s4uyou0001i2r47pxix6z1',
196+
name: 'test',
197+
providerIdentifier: 'instagram',
198+
picture: 'https://uploads.gitroom.com/F6LSCD8wrrQ.jpeg',
199+
type: 'social',
200+
},
201+
},
202+
{
203+
id: 'cm6tcts4f0005qcwit25cis26',
204+
content: 'This is the second post to facebook',
205+
publishDate: '2025-02-06T13:09:00.000Z',
206+
releaseURL: 'https://facebook.com/release2/release2',
207+
state: 'PUBLISHED',
208+
integration: {
209+
id: 'cm6s4uyou0001i2r47pxix6z1',
210+
name: 'test2',
211+
providerIdentifier: 'facebook',
212+
picture: 'https://uploads.gitroom.com/F6LSCD8wrrQ.jpeg',
213+
type: 'social',
214+
},
215+
},
216+
]),
217+
});
218+
} catch (e: any) {
219+
/** empty **/
220+
}
221+
}, []);
222+
178223
return (
179224
<FormProvider {...form}>
180225
<form onSubmit={form.handleSubmit(callBack)}>
@@ -227,16 +272,32 @@ export const AddOrEditWebhook: FC<{ data?: any; reload: () => void }> = (
227272
isMain={true}
228273
/>
229274
)}
230-
<Button
231-
type="submit"
232-
className="mt-[24px]"
233-
disabled={
234-
!form.formState.isValid ||
235-
(allIntegrations.value === 'specific' && !integrations?.length)
236-
}
237-
>
238-
Save
239-
</Button>
275+
<div className="flex gap-[10px]">
276+
<Button
277+
type="submit"
278+
className="mt-[24px]"
279+
disabled={
280+
!form.formState.isValid ||
281+
(allIntegrations.value === 'specific' &&
282+
!integrations?.length)
283+
}
284+
>
285+
Save
286+
</Button>
287+
<Button
288+
type="button"
289+
secondary={true}
290+
className="mt-[24px]"
291+
onClick={sendTest}
292+
disabled={
293+
!form.formState.isValid ||
294+
(allIntegrations.value === 'specific' &&
295+
!integrations?.length)
296+
}
297+
>
298+
Send Test
299+
</Button>
300+
</div>
240301
</div>
241302
</div>
242303
</form>

libraries/nestjs-libraries/src/database/prisma/webhooks/webhooks.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ export class WebhooksService {
7373
);
7474
}
7575

76-
sendList.push({
77-
url: webhook.url,
78-
data: toSend,
79-
});
76+
if (toSend.length) {
77+
sendList.push({
78+
url: webhook.url,
79+
data: toSend,
80+
});
81+
}
8082
}
8183

8284
return Promise.all(

0 commit comments

Comments
 (0)