Skip to content

Commit 68f80bc

Browse files
authored
fix: regression in esign flow (#304)
* chore: upgrade prisma * fix: schema * fix: recipient list * fix: redirection * fix: query * refactor: task * fix: add update for no trigger * fix: type * feat: query * fix: delete unused recipients * fix: email and logic * refactor: remove unused code * fix: recipient bug * fix: jobs
1 parent fe7dee7 commit 68f80bc

File tree

11 files changed

+192
-263
lines changed

11 files changed

+192
-263
lines changed

package-lock.json

Lines changed: 34 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
"postcss": "^8.4.38",
123123
"prettier": "^3.1.0",
124124
"prettier-plugin-tailwindcss": "^0.5.13",
125-
"prisma": "^5.12.1",
125+
"prisma": "^5.13.0",
126126
"tailwindcss": "^3.4.3",
127127
"tsx": "^4.7.0",
128128
"typescript": "^5.4.5"

src/components/template/template-field-form/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const TemplateFieldForm = ({
3333
});
3434

3535
if (status === "COMPLETE") {
36-
router.push(`/${companyPublicId}/documents`);
36+
router.push(`/${companyPublicId}/documents/esign`);
3737
}
3838
},
3939
});

src/jobs/esign-confirmation-email.ts

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,20 @@ import { client } from "@/trigger";
44
import { eventTrigger } from "@trigger.dev/sdk";
55
import { render } from "jsx-email";
66

7-
export interface TypeGeneralPayload {
7+
export type TConfirmationEmailPayload = {
88
fileUrl: string;
99
documentName: string;
1010
senderName: string | null;
1111
company: {
1212
name: string;
1313
logo?: string | null;
1414
};
15-
}
16-
17-
export interface TConfirmationEmailPayload extends TypeGeneralPayload {
18-
recipient: { name: string | null; email: string };
19-
}
20-
21-
export type TRecipientsKind = TypeGeneralPayload & {
22-
kind: "RECIPIENTS";
23-
recipients: { id: string; name: string | null; email: string }[];
24-
};
25-
26-
export type TRecipientKind = TypeGeneralPayload & {
27-
kind: "RECIPIENT";
28-
recipient: { id: string; name: string | null; email: string };
15+
recipient: { name?: string | null; email: string };
2916
};
3017

31-
export type TConfirmationEmailJobPayload = TRecipientKind | TRecipientsKind;
32-
33-
type Payload = TConfirmationEmailPayload;
34-
export const sendEsignConfirmationEmail = async (payload: Payload) => {
18+
export const sendEsignConfirmationEmail = async (
19+
payload: TConfirmationEmailPayload,
20+
) => {
3521
const html = await render(
3622
ESignConfirmationEmail({
3723
documentName: payload.documentName,
@@ -42,11 +28,11 @@ export const sendEsignConfirmationEmail = async (payload: Payload) => {
4228
);
4329
await sendMail({
4430
to: payload.recipient.email,
45-
subject: "Completed e-signed documents from all partiess",
31+
subject: "Completed e-signed documents from all parties",
4632
html,
4733
attachments: [
4834
{
49-
filename: "signed_document.pdf",
35+
filename: payload.documentName,
5036
path: payload.fileUrl,
5137
},
5238
],
@@ -61,40 +47,9 @@ client.defineJob({
6147
name: "esign.send-confirmation",
6248
}),
6349

64-
run: async (payload: TConfirmationEmailJobPayload, io) => {
65-
if (payload.kind === "RECIPIENT") {
66-
await io.runTask(
67-
`send-confirmation-email-${payload.recipient.id}`,
68-
async () => {
69-
await sendEsignConfirmationEmail({
70-
documentName: payload.documentName,
71-
senderName: payload.senderName,
72-
company: payload.company,
73-
fileUrl: payload.fileUrl,
74-
recipient: {
75-
name: payload.recipient?.name,
76-
email: payload.recipient.email,
77-
},
78-
});
79-
},
80-
);
81-
}
82-
83-
if (payload.kind === "RECIPIENTS") {
84-
for (const recipient of payload.recipients) {
85-
await io.runTask(`recpient-${recipient.id}`, async () => {
86-
await sendEsignConfirmationEmail({
87-
documentName: payload.documentName,
88-
senderName: payload.senderName,
89-
company: payload.company,
90-
fileUrl: payload.fileUrl,
91-
recipient: {
92-
name: recipient?.name,
93-
email: recipient.email,
94-
},
95-
});
96-
});
97-
}
98-
}
50+
run: async (payload: TConfirmationEmailPayload, io) => {
51+
await io.runTask("send confirmation email", async () => {
52+
await sendEsignConfirmationEmail(payload);
53+
});
9954
},
10055
});

src/jobs/esign-email.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import EsignEmail from "@/emails/EsignEmail";
22
import { env } from "@/env";
3-
import { EsignRecipientStatus } from "@/prisma/enums";
43
import { db } from "@/server/db";
54
import { sendMail } from "@/server/mailer";
65
import { client } from "@/trigger";
@@ -9,8 +8,8 @@ import { render } from "jsx-email";
98

109
export interface TEmailPayload {
1110
documentName?: string;
12-
message?: string;
13-
recipient?: {
11+
message?: string | null;
12+
recipient: {
1413
id: string;
1514
name: string | null | undefined;
1615
email: string;
@@ -57,18 +56,19 @@ client.defineJob({
5756
}),
5857

5958
run: async (payload: TEsignEmailJob, io) => {
60-
await io.runTask(`send esign email`, async () => {
59+
await io.runTask("send esign email", async () => {
6160
await sendEsignEmail(payload);
62-
if (payload?.recipient?.id) {
63-
await db.esignRecipient.update({
64-
where: {
65-
id: payload.recipient.id,
66-
},
67-
data: {
68-
status: EsignRecipientStatus.SENT,
69-
},
70-
});
71-
}
61+
});
62+
63+
await io.runTask("update recipient status", async () => {
64+
await db.esignRecipient.update({
65+
where: {
66+
id: payload.recipient.id,
67+
},
68+
data: {
69+
status: "SENT",
70+
},
71+
});
7272
});
7373
},
7474
});

src/jobs/esign-pdf.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { db } from "@/server/db";
22
import {
33
completeEsignDocuments,
44
generateEsignPdf,
5-
getEmailSpecificInfoFromTemplate,
65
uploadEsignDocuments,
76
type TEsignGetTemplate,
87
} from "@/server/esign";
98
import { getPresignedGetUrl } from "@/server/file-uploads";
109
import { client } from "@/trigger";
11-
import { invokeTrigger } from "@trigger.dev/sdk";
10+
import { eventTrigger } from "@trigger.dev/sdk";
1211
import { z } from "zod";
12+
import { type TConfirmationEmailPayload } from "./esign-confirmation-email";
1313

1414
const schema = z.object({
1515
bucketKey: z.string(),
@@ -22,20 +22,29 @@ const schema = z.object({
2222
templateId: z.string(),
2323
uploaderName: z.string(),
2424
userAgent: z.string(),
25+
recipients: z.array(
26+
z.object({ email: z.string(), name: z.string().nullish() }),
27+
),
28+
company: z.object({
29+
name: z.string(),
30+
logo: z.string().nullish(),
31+
}),
2532
});
2633

2734
export type TESignPdfSchema = Omit<z.infer<typeof schema>, "fields"> & {
2835
fields: TEsignGetTemplate["fields"];
2936
};
30-
type TPayload = z.infer<typeof schema>;
3137

32-
export const CompleteSignDocumentJob = client.defineJob({
38+
client.defineJob({
3339
id: "esign.complete-pdf",
3440
name: "esign_complete_pdf",
3541
version: "0.0.1",
36-
trigger: invokeTrigger(),
42+
trigger: eventTrigger({
43+
name: "esign.sign-pdf",
44+
schema,
45+
}),
3746

38-
run: async (payload: TPayload, io) => {
47+
run: async (payload, io) => {
3948
const {
4049
bucketKey,
4150
data,
@@ -47,6 +56,8 @@ export const CompleteSignDocumentJob = client.defineJob({
4756
userAgent,
4857
uploaderName,
4958
templateId,
59+
recipients,
60+
company,
5061
} = payload;
5162

5263
const uploadedDocument = await io.runTask("upload documents", async () => {
@@ -80,24 +91,22 @@ export const CompleteSignDocumentJob = client.defineJob({
8091
});
8192
});
8293

83-
await io.runTask("send-confirmation-email", async () => {
84-
const _fileUrl = await getPresignedGetUrl(
85-
uploadedDocument.bucketData.key,
86-
);
87-
const _payload = await getEmailSpecificInfoFromTemplate(templateId, db);
88-
const payload = {
89-
documentName: _payload.documentName,
90-
senderName: _payload.senderName,
91-
company: _payload.company,
92-
fileUrl: _fileUrl.url,
93-
recipients: _payload.recipients,
94-
kind: "RECIPIENTS",
95-
};
94+
await io.runTask("send all recipients confirmation email", async () => {
95+
const file = await getPresignedGetUrl(uploadedDocument.bucketData.key);
9696

97-
await io.sendEvent(`trigger-confirmation-email`, {
98-
name: "esign.send-confirmation",
99-
payload,
100-
});
97+
const data: { name: string; payload: TConfirmationEmailPayload }[] =
98+
recipients.map((recipient) => ({
99+
payload: {
100+
fileUrl: file.url,
101+
documentName: templateName,
102+
recipient,
103+
company,
104+
senderName: uploaderName,
105+
},
106+
name: "esign.send-confirmation",
107+
}));
108+
109+
await io.sendEvents(`trigger-confirmation-email`, data);
101110
});
102111
},
103112
});

src/providers/template-field-provider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const formSchema = z.object({
4242
.object({
4343
options: z
4444
.array(z.object({ id: z.string(), value: z.string() }))
45-
.nonempty(),
45+
.nonempty()
46+
.optional(),
4647
})
4748
.optional(),
4849
}),

0 commit comments

Comments
 (0)