Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app/api/messages/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MessageSchema } from '@/types/message';
import { MessageService } from '@/app/api/messages/services/message.service';
import appConfig from '@/config/app';
import { WebhookSchema } from '@/types/webhook';
import { hasTimeExceeded } from '@/utils/hasTimeExceeded';

export async function POST(request: NextRequest) {
const rawBody = await request.text();
Expand Down Expand Up @@ -48,6 +49,10 @@ export async function POST(request: NextRequest) {
);
}
}
if (hasTimeExceeded(payload.data.createdAt)) {
console.info('Autoresponse failed due to stale webhook timestamp. ', payload.data);
return NextResponse.json({});
}

const messageService = new MessageService();
await messageService.handleSendMessageWebhook(payload.data, {
Expand Down
2 changes: 2 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export enum AUTO_RESPONSE {
OFF = 'off',
}

export const AUTO_RESPONSE_TIME_THRESHOLD = 60;

export const AUTO_RESPONSE_OPTIONS: SelectOption<$Enums.SettingType>[] = [
{
value: $Enums.SettingType.OUTSIDE_WORKING_HOURS,
Expand Down
9 changes: 9 additions & 0 deletions src/utils/hasTimeExceeded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AUTO_RESPONSE_TIME_THRESHOLD } from '@/constants';

export function hasTimeExceeded(createdAt: string): boolean {
const thresholdInSeconds = AUTO_RESPONSE_TIME_THRESHOLD || 60;
const createdTime = new Date(createdAt).getTime();
const currentTime = Date.now();
const diffInSeconds = Math.abs(currentTime - createdTime) / 1000;
return diffInSeconds > thresholdInSeconds;
}
Loading