Skip to content

Commit

Permalink
feat: use nano id
Browse files Browse the repository at this point in the history
  • Loading branch information
G3root committed Aug 20, 2024
1 parent 07dc319 commit 103f839
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 78 deletions.
51 changes: 29 additions & 22 deletions src/trpc/routers/template-field-router/procedures/add-fields.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
/* eslint-disable @typescript-eslint/prefer-for-of */
import { customId } from "@/common/id";
import {
EsignNotificationEmailJob,
type ExtendedEsignPayloadType,
} from "@/jobs/esign-email";
import { decode, encode } from "@/lib/jwt";
import { Audit } from "@/server/audit";
import { checkMembership } from "@/server/auth";
import type { TPrismaOrTransaction } from "@/server/db";
import { withAuth } from "@/trpc/api/trpc";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { ZodAddFieldMutationSchema } from "../schema";

const emailTokenPayloadSchema = z.object({
id: z.string(),
rec: z.string(),
});

type TEmailTokenPayloadSchema = z.infer<typeof emailTokenPayloadSchema>;

interface EncodeEmailTokenOption {
interface TEncodeSignatureTokenOption {
db: TPrismaOrTransaction;
templateId: string;
recipientId: string;
}

export function EncodeEmailToken({
export async function encodeSignatureToken({
recipientId,
templateId,
}: EncodeEmailTokenOption) {
const encodeToken: TEmailTokenPayloadSchema = {
rec: recipientId,
id: templateId,
};
db,
}: TEncodeSignatureTokenOption) {
const token = customId();

await db.eSignToken.create({
data: {
templateId,
recipientId,
token,
},
});

return encode(encodeToken);
return token;
}

export async function DecodeEmailToken(jwt: string) {
const { payload } = await decode(jwt);
return emailTokenPayloadSchema.parse(payload);
export function decodeSignatureToken(token: string, db: TPrismaOrTransaction) {
return db.eSignToken.findFirstOrThrow({
where: {
token,
},
select: {
recipientId: true,
templateId: true,
},
});
}

export const addFieldProcedure = withAuth
Expand Down Expand Up @@ -165,9 +171,10 @@ export const addFieldProcedure = withAuth
throw new Error("not found");
}

const token = await EncodeEmailToken({
const token = await encodeSignatureToken({
recipientId: recipient.id,
templateId: template.id,
db: tx,
});

const email = recipient.email;
Expand Down
110 changes: 56 additions & 54 deletions src/trpc/routers/template-router/procedures/get-signing-fields.tsx
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
import { getPresignedGetUrl } from "@/server/file-uploads";
import { withoutAuth } from "@/trpc/api/trpc";
import { DecodeEmailToken } from "../../template-field-router/procedures/add-fields";
import { decodeSignatureToken } from "../../template-field-router/procedures/add-fields";
import { ZodGetSigningFieldsSchema } from "../schema";

export const getSigningFieldsProcedure = withoutAuth
.input(ZodGetSigningFieldsSchema)
.query(async ({ ctx, input }) => {
const { id: templateId, rec: recipientId } = await DecodeEmailToken(
input.token,
);
const { bucket, fields, status, recipientId, templateId } =
await ctx.db.$transaction(async (tx) => {
const { recipientId, templateId } = await decodeSignatureToken(
input.token,
tx,
);

const { bucket, fields, status } = await ctx.db.$transaction(async (tx) => {
const recipient = await tx.esignRecipient.findFirstOrThrow({
where: {
id: recipientId,
templateId,
status: "SENT",
},
select: {
templateId: true,
},
});
const recipient = await tx.esignRecipient.findFirstOrThrow({
where: {
id: recipientId,
templateId,
status: "SENT",
},
select: {
templateId: true,
},
});

const { bucket, status } = await tx.template.findFirstOrThrow({
where: {
id: recipient.templateId,
},
select: {
bucket: {
select: {
key: true,
const { bucket, status } = await tx.template.findFirstOrThrow({
where: {
id: recipient.templateId,
},
select: {
bucket: {
select: {
key: true,
},
},
status: true,
},
status: true,
},
});
});

const fields = await tx.templateField.findMany({
where: {
templateId: recipient.templateId,
},
select: {
id: true,
name: true,
width: true,
height: true,
top: true,
left: true,
required: true,
defaultValue: true,
readOnly: true,
type: true,
viewportHeight: true,
viewportWidth: true,
page: true,
recipientId: true,
prefilledValue: true,
meta: true,
},
orderBy: {
top: "asc",
},
});
const fields = await tx.templateField.findMany({
where: {
templateId: recipient.templateId,
},
select: {
id: true,
name: true,
width: true,
height: true,
top: true,
left: true,
required: true,
defaultValue: true,
readOnly: true,
type: true,
viewportHeight: true,
viewportWidth: true,
page: true,
recipientId: true,
prefilledValue: true,
meta: true,
},
orderBy: {
top: "asc",
},
});

return { bucket, fields, status };
});
return { bucket, fields, status, templateId, recipientId };
});

const { key, url } = await getPresignedGetUrl(bucket.key);

Expand Down
5 changes: 3 additions & 2 deletions src/trpc/routers/template-router/procedures/sign-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
type uploadEsignDocumentsOptions,
} from "@/server/esign";
import { withoutAuth } from "@/trpc/api/trpc";
import { EncodeEmailToken } from "../../template-field-router/procedures/add-fields";
import { encodeSignatureToken } from "../../template-field-router/procedures/add-fields";
import { SignTemplateMutationSchema } from "../schema";

export const signTemplateProcedure = withoutAuth
Expand Down Expand Up @@ -218,9 +218,10 @@ export const signTemplateProcedure = withoutAuth
},
});
if (nextDelivery) {
const token = await EncodeEmailToken({
const token = await encodeSignatureToken({
recipientId: nextDelivery.id,
templateId: template.id,
db: tx,
});
const email = nextDelivery.email;

Expand Down

0 comments on commit 103f839

Please sign in to comment.