Skip to content

The Open Source DocuSign Alternative.

chore: feedback from pull request

Mythie664b9284bd0f
diff --git a/packages/lib/jobs/client/local.ts b/packages/lib/jobs/client/local.ts
index 8f368c404..8a307f082 100644
--- a/packages/lib/jobs/client/local.ts
+++ b/packages/lib/jobs/client/local.ts
@@ -80,144 +80,144 @@ export class LocalJobProvider extends BaseJobProvider {
public getApiHandler() {
return async (req: NextApiRequest, res: NextApiResponse) => {
- if (req.method === 'POST') {
- const jobId = req.headers['x-job-id'];
- const signature = req.headers['x-job-signature'];
- const isRetry = req.headers['x-job-retry'] !== undefined;
+ if (req.method !== 'POST') {
+ res.status(405).send('Method not allowed');
+ }
+
+ const jobId = req.headers['x-job-id'];
+ const signature = req.headers['x-job-signature'];
+ const isRetry = req.headers['x-job-retry'] !== undefined;
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
+ const options = await json(req)
+ .then(async (data) => ZSimpleTriggerJobOptionsSchema.parseAsync(data))
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
- const options = await json(req)
- .then(async (data) => ZSimpleTriggerJobOptionsSchema.parseAsync(data))
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
- .then((data) => data as SimpleTriggerJobOptions)
- .catch(() => null);
+ .then((data) => data as SimpleTriggerJobOptions)
+ .catch(() => null);
- if (!options) {
- res.status(400).send('Bad request');
- return;
- }
+ if (!options) {
+ res.status(400).send('Bad request');
+ return;
+ }
- const definition = this._jobDefinitions[options.name];
+ const definition = this._jobDefinitions[options.name];
- if (
- typeof jobId !== 'string' ||
- typeof signature !== 'string' ||
- typeof options !== 'object'
- ) {
- res.status(400).send('Bad request');
- return;
- }
+ if (
+ typeof jobId !== 'string' ||
+ typeof signature !== 'string' ||
+ typeof options !== 'object'
+ ) {
+ res.status(400).send('Bad request');
+ return;
+ }
- if (!definition) {
- res.status(404).send('Job not found');
- return;
- }
+ if (!definition) {
+ res.status(404).send('Job not found');
+ return;
+ }
- if (definition && !definition.enabled) {
- console.log('Attempted to trigger a disabled job', options.name);
+ if (definition && !definition.enabled) {
+ console.log('Attempted to trigger a disabled job', options.name);
- res.status(404).send('Job not found');
- return;
- }
+ res.status(404).send('Job not found');
+ return;
+ }
- if (!signature || !verify(options, signature)) {
- res.status(401).send('Unauthorized');
- return;
- }
+ if (!signature || !verify(options, signature)) {
+ res.status(401).send('Unauthorized');
+ return;
+ }
- if (definition.trigger.schema) {
- const result = definition.trigger.schema.safeParse(options.payload);
+ if (definition.trigger.schema) {
+ const result = definition.trigger.schema.safeParse(options.payload);
- if (!result.success) {
- res.status(400).send('Bad request');
- return;
- }
+ if (!result.success) {
+ res.status(400).send('Bad request');
+ return;
}
+ }
- console.log(`[JOBS]: Triggering job ${options.name} with payload`, options.payload);
+ console.log(`[JOBS]: Triggering job ${options.name} with payload`, options.payload);
- let backgroundJob = await prisma.backgroundJob
- .update({
- where: {
- id: jobId,
- status: BackgroundJobStatus.PENDING,
- },
- data: {
- status: BackgroundJobStatus.PROCESSING,
- retried: {
- increment: isRetry ? 1 : 0,
- },
- lastRetriedAt: isRetry ? new Date() : undefined,
+ let backgroundJob = await prisma.backgroundJob
+ .update({
+ where: {
+ id: jobId,
+ status: BackgroundJobStatus.PENDING,
+ },
+ data: {
+ status: BackgroundJobStatus.PROCESSING,
+ retried: {
+ increment: isRetry ? 1 : 0,
},
- })
- .catch(() => null);
+ lastRetriedAt: isRetry ? new Date() : undefined,
+ },
+ })
+ .catch(() => null);
- if (!backgroundJob) {
- res.status(404).send('Job not found');
- return;
- }
+ if (!backgroundJob) {
+ res.status(404).send('Job not found');
+ return;
+ }
- try {
- await definition.handler({
- payload: options.payload,
- io: this.createJobRunIO(jobId),
- });
+ try {
+ await definition.handler({
+ payload: options.payload,
+ io: this.createJobRunIO(jobId),
+ });
- backgroundJob = await prisma.backgroundJob.update({
- where: {
- id: jobId,
- status: BackgroundJobStatus.PROCESSING,
- },
- data: {
- status: BackgroundJobStatus.COMPLETED,
- completedAt: new Date(),
- },
- });
- } catch (error) {
- console.error(`[JOBS]: Job ${options.name} failed`, error);
-
- const taskHasExceededRetries = error instanceof BackgroundTaskExceededRetriesError;
- const jobHasExceededRetries =
- backgroundJob.retried >= backgroundJob.maxRetries &&
- !(error instanceof BackgroundTaskFailedError);
-
- if (taskHasExceededRetries || jobHasExceededRetries) {
- backgroundJob = await prisma.backgroundJob.update({
- where: {
- id: jobId,
- status: BackgroundJobStatus.PROCESSING,
- },
- data: {
- status: BackgroundJobStatus.FAILED,
- completedAt: new Date(),
- },
- });
+ backgroundJob = await prisma.backgroundJob.update({
+ where: {
+ id: jobId,
+ status: BackgroundJobStatus.PROCESSING,
+ },
+ data: {
+ status: BackgroundJobStatus.COMPLETED,
+ completedAt: new Date(),
+ },
+ });
+ } catch (error) {
+ console.error(`[JOBS]: Job ${options.name} failed`, error);
- res.status(500).send('Task exceeded retries');
- return;
- }
+ const taskHasExceededRetries = error instanceof BackgroundTaskExceededRetriesError;
+ const jobHasExceededRetries =
+ backgroundJob.retried >= backgroundJob.maxRetries &&
+ !(error instanceof BackgroundTaskFailedError);
+ if (taskHasExceededRetries || jobHasExceededRetries) {
backgroundJob = await prisma.backgroundJob.update({
where: {
id: jobId,
status: BackgroundJobStatus.PROCESSING,
},
data: {
- status: BackgroundJobStatus.PENDING,
+ status: BackgroundJobStatus.FAILED,
+ completedAt: new Date(),
},
});
- await this.submitJobToEndpoint({
- jobId,
- jobDefinitionId: backgroundJob.jobId,
- data: options,
- });
+ res.status(500).send('Task exceeded retries');
+ return;
}
- res.status(200).send('OK');
- } else {
- res.status(405).send('Method not allowed');
+ backgroundJob = await prisma.backgroundJob.update({
+ where: {
+ id: jobId,
+ status: BackgroundJobStatus.PROCESSING,
+ },
+ data: {
+ status: BackgroundJobStatus.PENDING,
+ },
+ });
+
+ await this.submitJobToEndpoint({
+ jobId,
+ jobDefinitionId: backgroundJob.jobId,
+ data: options,
+ });
}
+
+ res.status(200).send('OK');
};
}
diff --git a/packages/lib/jobs/definitions/send-confirmation-email.ts b/packages/lib/jobs/definitions/send-confirmation-email.ts
index 7da5a88f1..b5ca6e43c 100644
--- a/packages/lib/jobs/definitions/send-confirmation-email.ts
+++ b/packages/lib/jobs/definitions/send-confirmation-email.ts
@@ -4,11 +4,11 @@ import { sendConfirmationToken } from '../../server-only/user/send-confirmation-
import type { JobDefinition } from '../client/_internal/job';
export const SEND_CONFIRMATION_EMAIL_JOB_DEFINITION = {
- id: 'send.confirmation.email',
+ id: 'send.signup.confirmation.email',
name: 'Send Confirmation Email',
version: '1.0.0',
trigger: {
- name: 'send.confirmation.email',
+ name: 'send.signup.confirmation.email',
schema: z.object({
email: z.string().email(),
force: z.boolean().optional(),
diff --git a/packages/lib/jobs/definitions/send-signing-email.ts b/packages/lib/jobs/definitions/send-signing-email.ts
index 5ee6c2fd2..88e228844 100644
--- a/packages/lib/jobs/definitions/send-signing-email.ts
+++ b/packages/lib/jobs/definitions/send-signing-email.ts
@@ -26,11 +26,11 @@ import { renderCustomEmailTemplate } from '../../utils/render-custom-email-templ
import type { JobDefinition } from '../client/_internal/job';
export const SEND_SIGNING_EMAIL_JOB_DEFINITION = {
- id: 'send.signing.email',
+ id: 'send.signing.requested.email',
name: 'Send Signing Email',
version: '1.0.0',
trigger: {
- name: 'send.signing.email',
+ name: 'send.signing.requested.email',
schema: z.object({
userId: z.number(),
documentId: z.number(),
diff --git a/packages/lib/next-auth/auth-options.ts b/packages/lib/next-auth/auth-options.ts
index 8df2cbc86..b0d27589c 100644
--- a/packages/lib/next-auth/auth-options.ts
+++ b/packages/lib/next-auth/auth-options.ts
@@ -109,7 +109,7 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
DateTime.fromJSDate(mostRecentToken.createdAt).diffNow('minutes').minutes > -5
) {
await jobsClient.triggerJob({
- name: 'send.confirmation.email',
+ name: 'send.signup.confirmation.email',
payload: {
email: user.email,
},
diff --git a/packages/lib/server-only/document/send-document.tsx b/packages/lib/server-only/document/send-document.tsx
index 83007e78a..9a6f24ce6 100644
--- a/packages/lib/server-only/document/send-document.tsx
+++ b/packages/lib/server-only/document/send-document.tsx
@@ -127,7 +127,7 @@ export const sendDocument = async ({
}
await jobsClient.triggerJob({
- name: 'send.signing.email',
+ name: 'send.signing.requested.email',
payload: {
userId,
documentId,
diff --git a/packages/lib/server-only/user/verify-email.ts b/packages/lib/server-only/user/verify-email.ts
index 3ffc258f9..1f3317b6e 100644
--- a/packages/lib/server-only/user/verify-email.ts
+++ b/packages/lib/server-only/user/verify-email.ts
@@ -41,7 +41,7 @@ export const verifyEmail = async ({ token }: VerifyEmailProps) => {
DateTime.now().minus({ hours: 1 }).toJSDate() > mostRecentToken.createdAt
) {
await jobsClient.triggerJob({
- name: 'send.confirmation.email',
+ name: 'send.signup.confirmation.email',
payload: {
email: verificationToken.user.email,
},
diff --git a/packages/trpc/server/auth-router/router.ts b/packages/trpc/server/auth-router/router.ts
index 62c1ee486..cbe851f62 100644
--- a/packages/trpc/server/auth-router/router.ts
+++ b/packages/trpc/server/auth-router/router.ts
@@ -53,7 +53,7 @@ export const authRouter = router({
const user = await createUser({ name, email, password, signature, url });
await jobsClient.triggerJob({
- name: 'send.confirmation.email',
+ name: 'send.signup.confirmation.email',
payload: {
email: user.email,
},
diff --git a/packages/trpc/server/profile-router/router.ts b/packages/trpc/server/profile-router/router.ts
index a66348234..921a0353c 100644
--- a/packages/trpc/server/profile-router/router.ts
+++ b/packages/trpc/server/profile-router/router.ts
@@ -201,7 +201,7 @@ export const profileRouter = router({
const { email } = input;
await jobsClient.triggerJob({
- name: 'send.confirmation.email',
+ name: 'send.signup.confirmation.email',
payload: {
email,
},