Skip to content

Commit

Permalink
Merge branch 'main' into fix-custom-fields-nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
sazanrjb committed Jan 5, 2024
2 parents 76e5565 + 0bec8fd commit 2c6a3cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
52 changes: 27 additions & 25 deletions src/app/api/messages/services/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,40 @@ export class MessageService {
return;
}

const client = await copilotClient.getClient(message.senderId);
try {
const client = await copilotClient.getClient(message.senderId);

if ('code' in client && client.code === 'parameter_invalid') {
return;
}

const clientMessageCount = await this.prismaClient.message.count({
where: {
channelId: message.channelId,
clientId: client.id,
createdAt: {
gte: this.subtractHours(new Date(), 1),
const clientMessageCount = await this.prismaClient.message.count({
where: {
channelId: message.channelId,
clientId: client.id,
createdAt: {
gte: this.subtractHours(new Date(), 1),
},
},
},
});
});

if (clientMessageCount > 0) {
return;
}
if (clientMessageCount > 0) {
return;
}

if (setting?.type === SettingType.ENABLED) {
await this.sendMessage(copilotClient, setting, message);
if (setting?.type === SettingType.ENABLED) {
await this.sendMessage(copilotClient, setting, message);

return;
}
return;
}

if (!setting?.timezone || !setting?.workingHours) {
return;
}
if (!setting?.timezone || !setting?.workingHours) {
return;
}

if (!isWithinWorkingHours(setting.timezone, setting.workingHours)) {
await this.sendMessage(copilotClient, setting, message);
}
} catch (e: unknown) {
console.error(e);

if (!isWithinWorkingHours(setting.timezone, setting.workingHours)) {
await this.sendMessage(copilotClient, setting, message);
return;
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/app/api/messages/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import { WebhookSchema } from '@/types/webhook';

export async function POST(request: NextRequest) {
const rawBody = await request.text();
const searchParams = new URL(request.url).searchParams;

const apiToken = searchParams.get('token');
if (!apiToken) {
return NextResponse.json(
{
message: 'Invalid token',
},
{ status: 400 },
);
}

const body = JSON.parse(rawBody);
const result = WebhookSchema.safeParse(body);
Expand Down Expand Up @@ -40,7 +51,7 @@ export async function POST(request: NextRequest) {

const messageService = new MessageService();
await messageService.handleSendMessageWebhook(payload.data, {
apiToken: data.token,
apiToken,
});

return NextResponse.json({});
Expand Down
1 change: 0 additions & 1 deletion src/types/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ export const WebhookSchema = z.object({
created: z.string().optional(),
object: z.string().optional(),
data: z.unknown(),
token: z.string(),
});

0 comments on commit 2c6a3cd

Please sign in to comment.