mirror of
https://github.com/documenso/documenso.git
synced 2026-06-23 04:42:09 +10:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f532c97499 | |||
| 9fc5ec11f4 | |||
| 8b771d36d2 |
@@ -203,7 +203,7 @@ export const EditDocumentForm = ({
|
||||
|
||||
const onAddSettingsFormSubmit = async (data: TAddSettingsFormSchema) => {
|
||||
try {
|
||||
const { timezone, dateFormat, redirectUrl, language } = data.meta;
|
||||
const { timezone, dateFormat, redirectUrl, language, reminderInterval } = data.meta;
|
||||
|
||||
await setSettingsForDocument({
|
||||
documentId: document.id,
|
||||
@@ -220,6 +220,7 @@ export const EditDocumentForm = ({
|
||||
dateFormat,
|
||||
redirectUrl,
|
||||
language: isValidLanguageCode(language) ? language : undefined,
|
||||
reminderInterval,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { JobClient } from './client/client';
|
||||
import { SEND_CONFIRMATION_EMAIL_JOB_DEFINITION } from './definitions/emails/send-confirmation-email';
|
||||
import { SEND_SIGNING_REJECTION_EMAILS_JOB_DEFINITION } from './definitions/emails/send-rejection-emails';
|
||||
import { SEND_SIGNING_EMAIL_JOB_DEFINITION } from './definitions/emails/send-signing-email';
|
||||
import { SEND_SIGNING_REMINDER_EMAIL_JOB } from './definitions/emails/send-signing-reminder-email';
|
||||
import { SEND_TEAM_DELETED_EMAIL_JOB_DEFINITION } from './definitions/emails/send-team-deleted-email';
|
||||
import { SEND_TEAM_MEMBER_JOINED_EMAIL_JOB_DEFINITION } from './definitions/emails/send-team-member-joined-email';
|
||||
import { SEND_TEAM_MEMBER_LEFT_EMAIL_JOB_DEFINITION } from './definitions/emails/send-team-member-left-email';
|
||||
@@ -19,6 +20,7 @@ export const jobsClient = new JobClient([
|
||||
SEND_TEAM_DELETED_EMAIL_JOB_DEFINITION,
|
||||
SEAL_DOCUMENT_JOB_DEFINITION,
|
||||
SEND_SIGNING_REJECTION_EMAILS_JOB_DEFINITION,
|
||||
SEND_SIGNING_REMINDER_EMAIL_JOB,
|
||||
] as const);
|
||||
|
||||
export const jobs = jobsClient;
|
||||
|
||||
@@ -26,16 +26,26 @@ export type TriggerJobOptions<Definitions extends ReadonlyArray<JobDefinition> =
|
||||
};
|
||||
}[number];
|
||||
|
||||
export type CronTrigger<N extends string = string> = {
|
||||
type: 'cron';
|
||||
schedule: string;
|
||||
name: N;
|
||||
};
|
||||
|
||||
export type EventTrigger<N extends string = string> = {
|
||||
type: 'event';
|
||||
name: N;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type JobDefinition<Name extends string = string, Schema = any> = {
|
||||
id: string;
|
||||
name: string;
|
||||
version: string;
|
||||
enabled?: boolean;
|
||||
trigger: {
|
||||
name: Name;
|
||||
schema?: z.ZodType<Schema>;
|
||||
};
|
||||
trigger:
|
||||
| (EventTrigger<Name> & { schema?: z.ZodType<Schema> })
|
||||
| (CronTrigger<Name> & { schema?: z.ZodType<Schema> });
|
||||
handler: (options: { payload: Schema; io: JobRunIO }) => Promise<Json | void>;
|
||||
};
|
||||
|
||||
|
||||
@@ -38,30 +38,58 @@ export class InngestJobProvider extends BaseJobProvider {
|
||||
|
||||
public defineJob<N extends string, T>(job: JobDefinition<N, T>): void {
|
||||
console.log('defining job', job.id);
|
||||
const fn = this._client.createFunction(
|
||||
{
|
||||
id: job.id,
|
||||
name: job.name,
|
||||
},
|
||||
{
|
||||
event: job.trigger.name,
|
||||
},
|
||||
async (ctx) => {
|
||||
const io = this.convertInngestIoToJobRunIo(ctx);
|
||||
|
||||
// We need to cast to any so we can deal with parsing later.
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
||||
let payload = ctx.event.data as any;
|
||||
if (job.trigger.type === 'cron') {
|
||||
const fn = this._client.createFunction(
|
||||
{
|
||||
id: job.id,
|
||||
name: job.name,
|
||||
},
|
||||
{
|
||||
cron: job.trigger.schedule,
|
||||
},
|
||||
async (ctx) => {
|
||||
const io = this.convertInngestIoToJobRunIo(ctx);
|
||||
|
||||
if (job.trigger.schema) {
|
||||
payload = job.trigger.schema.parse(payload);
|
||||
}
|
||||
// We need to cast to any so we can deal with parsing later.
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
||||
let payload = ctx.event.data as any;
|
||||
|
||||
await job.handler({ payload, io });
|
||||
},
|
||||
);
|
||||
if (job.trigger.schema) {
|
||||
payload = job.trigger.schema.parse(payload);
|
||||
}
|
||||
|
||||
this._functions.push(fn);
|
||||
await job.handler({ payload, io });
|
||||
},
|
||||
);
|
||||
|
||||
this._functions.push(fn);
|
||||
} else {
|
||||
const fn = this._client.createFunction(
|
||||
{
|
||||
id: job.id,
|
||||
name: job.name,
|
||||
},
|
||||
{
|
||||
event: job.trigger.name,
|
||||
},
|
||||
async (ctx) => {
|
||||
const io = this.convertInngestIoToJobRunIo(ctx);
|
||||
|
||||
// We need to cast to any so we can deal with parsing later.
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
||||
let payload = ctx.event.data as any;
|
||||
|
||||
if (job.trigger.schema) {
|
||||
payload = job.trigger.schema.parse(payload);
|
||||
}
|
||||
|
||||
await job.handler({ payload, io });
|
||||
},
|
||||
);
|
||||
|
||||
this._functions.push(fn);
|
||||
}
|
||||
}
|
||||
|
||||
public async triggerJob(options: SimpleTriggerJobOptions): Promise<void> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createPagesRoute } from '@trigger.dev/nextjs';
|
||||
import type { IO } from '@trigger.dev/sdk';
|
||||
import { TriggerClient, eventTrigger } from '@trigger.dev/sdk';
|
||||
import { TriggerClient, cronTrigger, eventTrigger } from '@trigger.dev/sdk';
|
||||
|
||||
import type { JobDefinition, JobRunIO, SimpleTriggerJobOptions } from './_internal/job';
|
||||
import { BaseJobProvider } from './base';
|
||||
@@ -31,16 +31,30 @@ export class TriggerJobProvider extends BaseJobProvider {
|
||||
}
|
||||
|
||||
public defineJob<N extends string, T>(job: JobDefinition<N, T>): void {
|
||||
this._client.defineJob({
|
||||
id: job.id,
|
||||
name: job.name,
|
||||
version: job.version,
|
||||
trigger: eventTrigger({
|
||||
name: job.trigger.name,
|
||||
schema: job.trigger.schema,
|
||||
}),
|
||||
run: async (payload, io) => job.handler({ payload, io: this.convertTriggerIoToJobRunIo(io) }),
|
||||
});
|
||||
if (job.trigger.type === 'cron') {
|
||||
this._client.defineJob({
|
||||
id: job.id,
|
||||
name: job.name,
|
||||
version: job.version,
|
||||
trigger: cronTrigger({
|
||||
cron: job.trigger.schedule,
|
||||
}),
|
||||
run: async (payload, io) =>
|
||||
job.handler({ payload: payload as T, io: this.convertTriggerIoToJobRunIo(io) }),
|
||||
});
|
||||
} else {
|
||||
this._client.defineJob({
|
||||
id: job.id,
|
||||
name: job.name,
|
||||
version: job.version,
|
||||
trigger: eventTrigger({
|
||||
name: job.trigger.name,
|
||||
schema: job.trigger.schema,
|
||||
}),
|
||||
run: async (payload, io) =>
|
||||
job.handler({ payload, io: this.convertTriggerIoToJobRunIo(io) }),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async triggerJob(options: SimpleTriggerJobOptions): Promise<void> {
|
||||
|
||||
@@ -15,6 +15,7 @@ export const SEND_CONFIRMATION_EMAIL_JOB_DEFINITION = {
|
||||
name: 'Send Confirmation Email',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'event',
|
||||
name: SEND_CONFIRMATION_EMAIL_JOB_DEFINITION_ID,
|
||||
schema: SEND_CONFIRMATION_EMAIL_JOB_DEFINITION_SCHEMA,
|
||||
},
|
||||
|
||||
@@ -30,6 +30,7 @@ export const SEND_SIGNING_REJECTION_EMAILS_JOB_DEFINITION = {
|
||||
name: 'Send Rejection Emails',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'event',
|
||||
name: SEND_SIGNING_REJECTION_EMAILS_JOB_DEFINITION_ID,
|
||||
schema: SEND_SIGNING_REJECTION_EMAILS_JOB_DEFINITION_SCHEMA,
|
||||
},
|
||||
|
||||
@@ -43,6 +43,7 @@ export const SEND_SIGNING_EMAIL_JOB_DEFINITION = {
|
||||
name: 'Send Signing Email',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'event',
|
||||
name: SEND_SIGNING_EMAIL_JOB_DEFINITION_ID,
|
||||
schema: SEND_SIGNING_EMAIL_JOB_DEFINITION_SCHEMA,
|
||||
},
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
import { createElement } from 'react';
|
||||
|
||||
import { msg } from '@lingui/macro';
|
||||
|
||||
import { mailer } from '@documenso/email/mailer';
|
||||
import DocumentInviteEmailTemplate from '@documenso/email/templates/document-invite';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { DocumentStatus, RecipientRole, SendStatus } from '@documenso/prisma/client';
|
||||
import { ReminderInterval, SigningStatus } from '@documenso/prisma/generated/types';
|
||||
|
||||
import { getI18nInstance } from '../../../client-only/providers/i18n.server';
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from '../../../constants/app';
|
||||
import { FROM_ADDRESS, FROM_NAME } from '../../../constants/email';
|
||||
import { RECIPIENT_ROLES_DESCRIPTION } from '../../../constants/recipient-roles';
|
||||
import { extractDerivedDocumentEmailSettings } from '../../../types/document-email';
|
||||
import { renderEmailWithI18N } from '../../../utils/render-email-with-i18n';
|
||||
import { shouldSendReminder } from '../../../utils/should-send-reminder';
|
||||
import type { JobDefinition, JobRunIO } from '../../client/_internal/job';
|
||||
|
||||
export type SendSigningReminderEmailHandlerOptions = {
|
||||
io: JobRunIO;
|
||||
};
|
||||
|
||||
const SEND_SIGNING_REMINDER_EMAIL_JOB_ID = 'send.signing.reminder.email';
|
||||
|
||||
export const SEND_SIGNING_REMINDER_EMAIL_JOB = {
|
||||
id: SEND_SIGNING_REMINDER_EMAIL_JOB_ID,
|
||||
name: 'Send Signing Reminder Email',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'cron',
|
||||
schedule: '*/5 * * * *',
|
||||
name: SEND_SIGNING_REMINDER_EMAIL_JOB_ID,
|
||||
},
|
||||
handler: async ({ io }) => {
|
||||
const now = new Date();
|
||||
|
||||
const documentWithReminders = await prisma.document.findMany({
|
||||
where: {
|
||||
status: DocumentStatus.PENDING,
|
||||
documentMeta: {
|
||||
reminderInterval: {
|
||||
not: ReminderInterval.NONE,
|
||||
},
|
||||
},
|
||||
deletedAt: null,
|
||||
},
|
||||
|
||||
include: {
|
||||
documentMeta: true,
|
||||
User: true,
|
||||
Recipient: {
|
||||
where: {
|
||||
signingStatus: SigningStatus.NOT_SIGNED,
|
||||
role: {
|
||||
not: RecipientRole.CC,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log(documentWithReminders);
|
||||
|
||||
for (const document of documentWithReminders) {
|
||||
if (!extractDerivedDocumentEmailSettings(document.documentMeta).recipientSigningRequest) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { documentMeta } = document;
|
||||
if (!documentMeta) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { reminderInterval, lastReminderSentAt } = documentMeta;
|
||||
if (
|
||||
!shouldSendReminder({
|
||||
reminderInterval,
|
||||
lastReminderSentAt,
|
||||
now,
|
||||
})
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const recipient of document.Recipient) {
|
||||
const i18n = await getI18nInstance(document.documentMeta?.language);
|
||||
const recipientActionVerb = i18n
|
||||
._(RECIPIENT_ROLES_DESCRIPTION[recipient.role].actionVerb)
|
||||
.toLowerCase();
|
||||
|
||||
const emailSubject = i18n._(
|
||||
msg`Reminder: Please ${recipientActionVerb} the document "${document.title}"`,
|
||||
);
|
||||
const emailMessage = i18n._(
|
||||
msg`This is a reminder to ${recipientActionVerb} the document "${document.title}". Please complete this at your earliest convenience.`,
|
||||
);
|
||||
|
||||
const signDocumentLink = `${NEXT_PUBLIC_WEBAPP_URL()}/sign/${recipient.token}`;
|
||||
const assetBaseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
||||
|
||||
const template = createElement(DocumentInviteEmailTemplate, {
|
||||
documentName: document.title,
|
||||
inviterName: document.User.name || undefined,
|
||||
inviterEmail: document.User.email,
|
||||
assetBaseUrl,
|
||||
signDocumentLink,
|
||||
customBody: emailMessage,
|
||||
role: recipient.role,
|
||||
selfSigner: recipient.email === document.User.email,
|
||||
});
|
||||
|
||||
await io.runTask('send-reminder-email', async () => {
|
||||
const [html, text] = await Promise.all([
|
||||
renderEmailWithI18N(template, { lang: document.documentMeta?.language }),
|
||||
renderEmailWithI18N(template, {
|
||||
lang: document.documentMeta?.language,
|
||||
plainText: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
await mailer.sendMail({
|
||||
to: {
|
||||
name: recipient.name,
|
||||
address: recipient.email,
|
||||
},
|
||||
from: {
|
||||
name: FROM_NAME,
|
||||
address: FROM_ADDRESS,
|
||||
},
|
||||
subject: emailSubject,
|
||||
html,
|
||||
text,
|
||||
});
|
||||
});
|
||||
|
||||
await io.runTask('update-recipient-status', async () => {
|
||||
await prisma.recipient.update({
|
||||
where: { id: recipient.id },
|
||||
data: { sendStatus: SendStatus.SENT },
|
||||
});
|
||||
});
|
||||
|
||||
// TODO: Duncan == Audit log
|
||||
// await io.runTask('store-reminder-audit-log', async () => {
|
||||
// await prisma.documentAuditLog.create({
|
||||
// data: createDocumentAuditLogData({
|
||||
// type: DOCUMENT_AUDIT_LOG_TYPE.REMINDER_SENT,
|
||||
// documentId: document.id,
|
||||
// user,
|
||||
// requestMetadata,
|
||||
// data: {
|
||||
// recipientId: recipient.id,
|
||||
// recipientName: recipient.name,
|
||||
// recipientEmail: recipient.email,
|
||||
// recipientRole: recipient.role,
|
||||
// },
|
||||
// }),
|
||||
// });
|
||||
// });
|
||||
}
|
||||
|
||||
await prisma.documentMeta.update({
|
||||
where: { id: document.documentMeta?.id },
|
||||
data: { lastReminderSentAt: now },
|
||||
});
|
||||
}
|
||||
},
|
||||
} as const satisfies JobDefinition<typeof SEND_SIGNING_REMINDER_EMAIL_JOB_ID>;
|
||||
@@ -40,6 +40,7 @@ export const SEND_TEAM_DELETED_EMAIL_JOB_DEFINITION = {
|
||||
name: 'Send Team Deleted Email',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'event',
|
||||
name: SEND_TEAM_DELETED_EMAIL_JOB_DEFINITION_ID,
|
||||
schema: SEND_TEAM_DELETED_EMAIL_JOB_DEFINITION_SCHEMA,
|
||||
},
|
||||
|
||||
@@ -27,6 +27,7 @@ export const SEND_TEAM_MEMBER_JOINED_EMAIL_JOB_DEFINITION = {
|
||||
name: 'Send Team Member Joined Email',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'event',
|
||||
name: SEND_TEAM_MEMBER_JOINED_EMAIL_JOB_DEFINITION_ID,
|
||||
schema: SEND_TEAM_MEMBER_JOINED_EMAIL_JOB_DEFINITION_SCHEMA,
|
||||
},
|
||||
|
||||
@@ -27,6 +27,7 @@ export const SEND_TEAM_MEMBER_LEFT_EMAIL_JOB_DEFINITION = {
|
||||
name: 'Send Team Member Left Email',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'event',
|
||||
name: SEND_TEAM_MEMBER_LEFT_EMAIL_JOB_DEFINITION_ID,
|
||||
schema: SEND_TEAM_MEMBER_LEFT_EMAIL_JOB_DEFINITION_SCHEMA,
|
||||
},
|
||||
|
||||
@@ -41,6 +41,7 @@ export const SEAL_DOCUMENT_JOB_DEFINITION = {
|
||||
name: 'Seal Document',
|
||||
version: '1.0.0',
|
||||
trigger: {
|
||||
type: 'event',
|
||||
name: SEAL_DOCUMENT_JOB_DEFINITION_ID,
|
||||
schema: SEAL_DOCUMENT_JOB_DEFINITION_SCHEMA,
|
||||
},
|
||||
|
||||
@@ -7,7 +7,11 @@ import {
|
||||
diffDocumentMetaChanges,
|
||||
} from '@documenso/lib/utils/document-audit-logs';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import type { DocumentDistributionMethod, DocumentSigningOrder } from '@documenso/prisma/client';
|
||||
import type {
|
||||
DocumentDistributionMethod,
|
||||
DocumentSigningOrder,
|
||||
ReminderInterval,
|
||||
} from '@documenso/prisma/client';
|
||||
|
||||
import type { SupportedLanguageCodes } from '../../constants/i18n';
|
||||
import type { TDocumentEmailSettings } from '../../types/document-email';
|
||||
@@ -24,6 +28,7 @@ export type CreateDocumentMetaOptions = {
|
||||
signingOrder?: DocumentSigningOrder;
|
||||
distributionMethod?: DocumentDistributionMethod;
|
||||
typedSignatureEnabled?: boolean;
|
||||
reminderInterval?: ReminderInterval;
|
||||
language?: SupportedLanguageCodes;
|
||||
userId: number;
|
||||
requestMetadata: RequestMetadata;
|
||||
@@ -42,6 +47,7 @@ export const upsertDocumentMeta = async ({
|
||||
emailSettings,
|
||||
distributionMethod,
|
||||
typedSignatureEnabled,
|
||||
reminderInterval,
|
||||
language,
|
||||
requestMetadata,
|
||||
}: CreateDocumentMetaOptions) => {
|
||||
@@ -96,6 +102,7 @@ export const upsertDocumentMeta = async ({
|
||||
emailSettings,
|
||||
distributionMethod,
|
||||
typedSignatureEnabled,
|
||||
reminderInterval,
|
||||
language,
|
||||
},
|
||||
update: {
|
||||
@@ -109,6 +116,7 @@ export const upsertDocumentMeta = async ({
|
||||
emailSettings,
|
||||
distributionMethod,
|
||||
typedSignatureEnabled,
|
||||
reminderInterval,
|
||||
language,
|
||||
},
|
||||
});
|
||||
@@ -123,7 +131,7 @@ export const upsertDocumentMeta = async ({
|
||||
user,
|
||||
requestMetadata,
|
||||
data: {
|
||||
changes: diffDocumentMetaChanges(originalDocumentMeta ?? {}, upsertedDocumentMeta),
|
||||
changes,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -8,8 +8,8 @@ import {
|
||||
RecipientRole,
|
||||
SendStatus,
|
||||
SigningStatus,
|
||||
WebhookTriggerEvents,
|
||||
} from '@documenso/prisma/client';
|
||||
import { WebhookTriggerEvents } from '@documenso/prisma/client';
|
||||
|
||||
import { jobs } from '../../jobs/client';
|
||||
import type { TRecipientActionAuth } from '../../types/document-auth';
|
||||
@@ -178,6 +178,10 @@ export const completeDocumentWithToken = async ({
|
||||
requestMetadata,
|
||||
},
|
||||
});
|
||||
|
||||
// TODO: Duncan -- trigger cron job to send reminder email
|
||||
// TODO: Duncan -- audit log
|
||||
// TODO: Trigger cron job if cron is activated
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import {
|
||||
RecipientRole,
|
||||
SendStatus,
|
||||
SigningStatus,
|
||||
WebhookTriggerEvents,
|
||||
} from '@documenso/prisma/client';
|
||||
import { WebhookTriggerEvents } from '@documenso/prisma/client';
|
||||
|
||||
import { jobs } from '../../jobs/client';
|
||||
import { extractDerivedDocumentEmailSettings } from '../../types/document-email';
|
||||
@@ -180,6 +180,16 @@ export const sendDocument = async ({
|
||||
requestMetadata,
|
||||
},
|
||||
});
|
||||
|
||||
// TODO: Duncan: Audit Log
|
||||
await jobs.triggerJob({
|
||||
name: 'send.signing.reminder.email',
|
||||
payload: {
|
||||
documentId,
|
||||
recipientId: recipient.id,
|
||||
initialDelay: 24 * 60 * 60 * 1000, // 24 hours in milliseconds
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
import { ReminderInterval } from '@documenso/prisma/client';
|
||||
|
||||
export type ShouldSendReminderOptions = {
|
||||
reminderInterval: ReminderInterval;
|
||||
lastReminderSentAt: Date | null;
|
||||
now: Date;
|
||||
};
|
||||
|
||||
export const shouldSendReminder = ({
|
||||
lastReminderSentAt,
|
||||
now = new Date(),
|
||||
reminderInterval,
|
||||
}: ShouldSendReminderOptions): boolean => {
|
||||
if (!lastReminderSentAt) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const hoursSinceLastReminder = DateTime.fromJSDate(now).diff(
|
||||
DateTime.fromJSDate(lastReminderSentAt),
|
||||
'hours',
|
||||
).hours;
|
||||
const monthsSinceLastReminder = DateTime.fromJSDate(now).diff(
|
||||
DateTime.fromJSDate(lastReminderSentAt),
|
||||
'months',
|
||||
).months;
|
||||
|
||||
switch (reminderInterval) {
|
||||
case ReminderInterval.EVERY_1_HOUR:
|
||||
return hoursSinceLastReminder >= 1;
|
||||
case ReminderInterval.EVERY_6_HOURS:
|
||||
return hoursSinceLastReminder >= 6;
|
||||
case ReminderInterval.EVERY_12_HOURS:
|
||||
return hoursSinceLastReminder >= 12;
|
||||
case ReminderInterval.DAILY:
|
||||
return hoursSinceLastReminder >= 24;
|
||||
case ReminderInterval.EVERY_3_DAYS:
|
||||
return hoursSinceLastReminder >= 72;
|
||||
case ReminderInterval.WEEKLY:
|
||||
return hoursSinceLastReminder >= 168;
|
||||
case ReminderInterval.EVERY_2_WEEKS:
|
||||
return hoursSinceLastReminder >= 336;
|
||||
case ReminderInterval.MONTHLY:
|
||||
return monthsSinceLastReminder >= 1;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
};
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "DocumentMeta" ADD COLUMN "reminderDays" INTEGER DEFAULT 0;
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `reminderDays` on the `DocumentMeta` table. All the data in the column will be lost.
|
||||
|
||||
*/
|
||||
-- CreateEnum
|
||||
CREATE TYPE "ReminderInterval" AS ENUM ('NONE', 'EVERY_1_HOUR', 'EVERY_6_HOURS', 'EVERY_12_HOURS', 'DAILY', 'EVERY_3_DAYS', 'WEEKLY', 'EVERY_2_WEEKS', 'MONTHLY');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "DocumentMeta" DROP COLUMN "reminderDays",
|
||||
ADD COLUMN "reminderInterval" "ReminderInterval" NOT NULL DEFAULT 'NONE';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "DocumentMeta" ADD COLUMN "lastReminderSentAt" TIMESTAMP(3);
|
||||
@@ -363,6 +363,18 @@ enum DocumentDistributionMethod {
|
||||
NONE
|
||||
}
|
||||
|
||||
enum ReminderInterval {
|
||||
NONE
|
||||
EVERY_1_HOUR
|
||||
EVERY_6_HOURS
|
||||
EVERY_12_HOURS
|
||||
DAILY
|
||||
EVERY_3_DAYS
|
||||
WEEKLY
|
||||
EVERY_2_WEEKS
|
||||
MONTHLY
|
||||
}
|
||||
|
||||
model DocumentMeta {
|
||||
id String @id @default(cuid())
|
||||
subject String?
|
||||
@@ -378,6 +390,8 @@ model DocumentMeta {
|
||||
language String @default("en")
|
||||
distributionMethod DocumentDistributionMethod @default(EMAIL)
|
||||
emailSettings Json?
|
||||
reminderInterval ReminderInterval @default(NONE)
|
||||
lastReminderSentAt DateTime?
|
||||
}
|
||||
|
||||
enum ReadStatus {
|
||||
|
||||
@@ -259,13 +259,20 @@ export const documentRouter = router({
|
||||
|
||||
const requestMetadata = extractNextApiRequestMetadata(ctx.req);
|
||||
|
||||
if (meta.timezone || meta.dateFormat || meta.redirectUrl) {
|
||||
if (
|
||||
meta.timezone ||
|
||||
meta.dateFormat ||
|
||||
meta.redirectUrl ||
|
||||
meta.language ||
|
||||
meta.reminderInterval
|
||||
) {
|
||||
await upsertDocumentMeta({
|
||||
documentId,
|
||||
dateFormat: meta.dateFormat,
|
||||
timezone: meta.timezone,
|
||||
redirectUrl: meta.redirectUrl,
|
||||
language: meta.language,
|
||||
reminderInterval: meta.reminderInterval,
|
||||
userId: ctx.user.id,
|
||||
requestMetadata,
|
||||
});
|
||||
@@ -420,7 +427,8 @@ export const documentRouter = router({
|
||||
meta.dateFormat ||
|
||||
meta.redirectUrl ||
|
||||
meta.distributionMethod ||
|
||||
meta.emailSettings
|
||||
meta.emailSettings ||
|
||||
meta.reminderInterval
|
||||
) {
|
||||
await upsertDocumentMeta({
|
||||
documentId,
|
||||
@@ -430,6 +438,7 @@ export const documentRouter = router({
|
||||
timezone: meta.timezone,
|
||||
redirectUrl: meta.redirectUrl,
|
||||
distributionMethod: meta.distributionMethod,
|
||||
reminderInterval: meta.reminderInterval,
|
||||
userId: ctx.user.id,
|
||||
emailSettings: meta.emailSettings,
|
||||
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
DocumentVisibility,
|
||||
FieldType,
|
||||
RecipientRole,
|
||||
ReminderInterval,
|
||||
} from '@documenso/prisma/client';
|
||||
|
||||
export const ZFindDocumentsQuerySchema = ZBaseTableSearchParamsSchema.extend({
|
||||
@@ -98,6 +99,7 @@ export const ZSetSettingsForDocumentMutationSchema = z.object({
|
||||
'Please enter a valid URL, make sure you include http:// or https:// part of the url.',
|
||||
}),
|
||||
language: z.enum(SUPPORTED_LANGUAGE_CODES).optional(),
|
||||
reminderInterval: z.nativeEnum(ReminderInterval).optional().default(ReminderInterval.NONE),
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -167,6 +169,7 @@ export const ZSendDocumentMutationSchema = z.object({
|
||||
'Please enter a valid URL, make sure you include http:// or https:// part of the url.',
|
||||
}),
|
||||
emailSettings: ZDocumentEmailSettingsSchema.optional(),
|
||||
reminderInterval: z.nativeEnum(ReminderInterval).optional().default(ReminderInterval.NONE),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import { DEFAULT_DOCUMENT_TIME_ZONE, TIME_ZONES } from '@documenso/lib/constants
|
||||
import { extractDocumentAuthMethods } from '@documenso/lib/utils/document-auth';
|
||||
import type { TeamMemberRole } from '@documenso/prisma/client';
|
||||
import { DocumentStatus, type Field, type Recipient, SendStatus } from '@documenso/prisma/client';
|
||||
import { ReminderInterval } from '@documenso/prisma/generated/types';
|
||||
import type { DocumentWithData } from '@documenso/prisma/types/document-with-data';
|
||||
import {
|
||||
DocumentGlobalAuthAccessSelect,
|
||||
@@ -100,6 +101,7 @@ export const AddSettingsFormPartial = ({
|
||||
?.value ?? DEFAULT_DOCUMENT_DATE_FORMAT,
|
||||
redirectUrl: document.documentMeta?.redirectUrl ?? '',
|
||||
language: document.documentMeta?.language ?? 'en',
|
||||
reminderInterval: document.documentMeta?.reminderInterval ?? ReminderInterval.NONE,
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -390,6 +392,59 @@ export const AddSettingsFormPartial = ({
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="meta.reminderInterval"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="flex flex-row items-center">
|
||||
<Trans>Reminder Interval</Trans>{' '}
|
||||
<Tooltip>
|
||||
<TooltipTrigger>
|
||||
<InfoIcon className="mx-2 h-4 w-4" />
|
||||
</TooltipTrigger>
|
||||
|
||||
<TooltipContent className="text-muted-foreground max-w-xs">
|
||||
<Trans>Set the interval between reminders for this document.</Trans>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</FormLabel>
|
||||
|
||||
<FormControl>
|
||||
<Select {...field} onValueChange={field.onChange}>
|
||||
<SelectTrigger className="bg-background">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
|
||||
<SelectContent>
|
||||
<SelectItem value={ReminderInterval.NONE}>No reminders</SelectItem>
|
||||
<SelectItem value={ReminderInterval.EVERY_1_HOUR}>
|
||||
Every hour
|
||||
</SelectItem>
|
||||
<SelectItem value={ReminderInterval.EVERY_6_HOURS}>
|
||||
Every 6 hours
|
||||
</SelectItem>
|
||||
<SelectItem value={ReminderInterval.EVERY_12_HOURS}>
|
||||
Every 12 hours
|
||||
</SelectItem>
|
||||
<SelectItem value={ReminderInterval.DAILY}>Daily</SelectItem>
|
||||
<SelectItem value={ReminderInterval.EVERY_3_DAYS}>
|
||||
Every 3 days
|
||||
</SelectItem>
|
||||
<SelectItem value={ReminderInterval.WEEKLY}>Weekly</SelectItem>
|
||||
<SelectItem value={ReminderInterval.EVERY_2_WEEKS}>
|
||||
Every 2 weeks
|
||||
</SelectItem>
|
||||
<SelectItem value={ReminderInterval.MONTHLY}>Monthly</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FormControl>
|
||||
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
} from '@documenso/lib/types/document-auth';
|
||||
import { isValidRedirectUrl } from '@documenso/lib/utils/is-valid-redirect-url';
|
||||
import { DocumentVisibility } from '@documenso/prisma/client';
|
||||
import { ReminderInterval } from '@documenso/prisma/generated/types';
|
||||
|
||||
export const ZMapNegativeOneToUndefinedSchema = z
|
||||
.string()
|
||||
@@ -45,6 +46,7 @@ export const ZAddSettingsFormSchema = z.object({
|
||||
.union([z.string(), z.enum(SUPPORTED_LANGUAGE_CODES)])
|
||||
.optional()
|
||||
.default('en'),
|
||||
reminderInterval: z.nativeEnum(ReminderInterval).optional().default(ReminderInterval.NONE),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user