feat: send tests

This commit is contained in:
Nevo David 2025-02-06 23:15:50 +07:00
parent d57448629d
commit d7764faf15
3 changed files with 102 additions and 15 deletions

View File

@ -1,4 +1,13 @@
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
Query,
} from '@nestjs/common';
import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';
import { Organization } from '@prisma/client';
import { ApiTags } from '@nestjs/swagger';
@ -47,4 +56,19 @@ export class WebhookController {
) {
return this._webhooksService.deleteWebhook(org.id, id);
}
@Post('/send')
async sendWebhook(@Body() body: any, @Query('url') url: string) {
try {
await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
});
} catch (err) {
/** sent **/
}
return { send: true };
}
}

View File

@ -175,6 +175,51 @@ export const AddOrEditWebhook: FC<{ data?: any; reload: () => void }> = (
[data, integrations]
);
const sendTest = useCallback(async () => {
const url = form.getValues('url');
toast.show('Webhook send', 'success');
try {
await fetch(`/webhooks/send?url=${encodeURIComponent(url)}`, {
method: 'POST',
headers: {
contentType: 'application/json',
},
body: JSON.stringify([
{
id: 'cm6tcts4f0005qcwit25cis26',
content: 'This is the first post to instagram',
publishDate: '2025-02-06T13:09:00.000Z',
releaseURL: 'https://facebook.com/release/release',
state: 'PUBLISHED',
integration: {
id: 'cm6s4uyou0001i2r47pxix6z1',
name: 'test',
providerIdentifier: 'instagram',
picture: 'https://uploads.gitroom.com/F6LSCD8wrrQ.jpeg',
type: 'social',
},
},
{
id: 'cm6tcts4f0005qcwit25cis26',
content: 'This is the second post to facebook',
publishDate: '2025-02-06T13:09:00.000Z',
releaseURL: 'https://facebook.com/release2/release2',
state: 'PUBLISHED',
integration: {
id: 'cm6s4uyou0001i2r47pxix6z1',
name: 'test2',
providerIdentifier: 'facebook',
picture: 'https://uploads.gitroom.com/F6LSCD8wrrQ.jpeg',
type: 'social',
},
},
]),
});
} catch (e: any) {
/** empty **/
}
}, []);
return (
<FormProvider {...form}>
<form onSubmit={form.handleSubmit(callBack)}>
@ -227,16 +272,32 @@ export const AddOrEditWebhook: FC<{ data?: any; reload: () => void }> = (
isMain={true}
/>
)}
<Button
type="submit"
className="mt-[24px]"
disabled={
!form.formState.isValid ||
(allIntegrations.value === 'specific' && !integrations?.length)
}
>
Save
</Button>
<div className="flex gap-[10px]">
<Button
type="submit"
className="mt-[24px]"
disabled={
!form.formState.isValid ||
(allIntegrations.value === 'specific' &&
!integrations?.length)
}
>
Save
</Button>
<Button
type="button"
secondary={true}
className="mt-[24px]"
onClick={sendTest}
disabled={
!form.formState.isValid ||
(allIntegrations.value === 'specific' &&
!integrations?.length)
}
>
Send Test
</Button>
</div>
</div>
</div>
</form>

View File

@ -73,10 +73,12 @@ export class WebhooksService {
);
}
sendList.push({
url: webhook.url,
data: toSend,
});
if (toSend.length) {
sendList.push({
url: webhook.url,
data: toSend,
});
}
}
return Promise.all(