Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

8.4 KB
Raw Blame
import z from "zod";
import { COVER_LETTER_VERSION_KINDS } from "@reactive-resume/db/schema";
import { aiOperationIdsSchema, aiUsageSummarySchema } from "@reactive-resume/schema/ai-usage";
import {
	coverLetterContentSchema,
	coverLetterDocumentSchema,
	coverLetterLayoutSchema,
	coverLetterSchema,
	coverLetterStyleSchema,
} from "@reactive-resume/schema/cover-letter/data";
import { templateSchema } from "@reactive-resume/schema/templates";
import { paginationShape } from "../pagination";
import { proposalApplicationsSchema, proposalSafetySchema } from "./proposals";

const idSchema = z.object({ id: z.string().min(1) });
const revisionSchema = idSchema.extend({ expectedRevision: z.number().int().min(1) });
const editableSchema = coverLetterContentSchema.pick({ name: true, recipient: true, content: true }).extend({
	greeting: coverLetterContentSchema.shape.greeting.unwrap().optional(),
	signOff: coverLetterContentSchema.shape.signOff.unwrap().optional(),
});
const recipientFieldsSchema = coverLetterContentSchema.pick({
	recipientName: true,
	recipientCompany: true,
	letterDate: true,
});

const letterVersionSummarySchema = z.object({
	id: z.string(),
	kind: z
		.enum(COVER_LETTER_VERSION_KINDS)
		.describe("What made the version: created, auto (an editing session), named, before-restore, restored or sent."),
	name: z.string().nullable().describe("A named version's name, or the company a sent one went to."),
	createdAt: z.date(),
});

const letterVersionSchema = letterVersionSummarySchema.extend({
	aiUsage: aiUsageSummarySchema.nullable().optional(),
	data: z.object({
		aiOperationIds: aiOperationIdsSchema.optional(),
		name: z.string(),
		recipient: z.string(),
		content: z.string(),
		greeting: coverLetterContentSchema.shape.greeting,
		signOff: coverLetterContentSchema.shape.signOff,
		style: coverLetterStyleSchema,
		layout: coverLetterLayoutSchema,
		recipientName: z.string(),
		recipientCompany: z.string(),
		letterDate: z.string().nullable(),
	}),
});
const versionRefSchema = idSchema.extend({ versionId: z.string().min(1) });
const versionNameSchema = z.string().trim().min(1).max(100);
export const coverLetterDraftChunkSchema = z.union([z.string(), z.object({ aiOperationId: z.string().min(1) })]);

export const coverLetterDto = {
	list: {
		input: z
			.object({
				search: z.string().max(100).optional(),
				resumeId: z.string().min(1).optional(),
				applicationId: z.string().min(1).optional(),
				limit: z.number().int().min(1).max(100).default(20),
				offset: z.number().int().min(0).default(0),
			})
			.default({ limit: 20, offset: 0 }),
		output: z.object({ items: z.array(coverLetterSchema), total: z.number() }),
	},
	getById: { input: idSchema, output: coverLetterSchema },
	create: {
		input: editableSchema.extend({
			recipient: editableSchema.shape.recipient.default(""),
			content: editableSchema.shape.content.default(""),
			resumeId: z.string().min(1).optional().describe("The resume it goes with; its details and design are linked."),
			applicationId: z.string().min(1).optional().describe("The job it's for; it fills the recipient."),
			sourceLetterId: z
				.string()
				.min(1)
				.optional()
				.describe(
					"An earlier owned letter to start from. Its body is copied; the new letter still takes its recipient, date, resume and application link from this request, and keeps its own version history.",
				),
			template: templateSchema.optional(),
			layout: coverLetterLayoutSchema
				.optional()
				.describe("Defaults to structured, or freeform when a recipient block is given."),
			recipientName: recipientFieldsSchema.shape.recipientName.unwrap().optional(),
			recipientCompany: recipientFieldsSchema.shape.recipientCompany.unwrap().optional(),
			letterDate: recipientFieldsSchema.shape.letterDate.unwrap().optional().describe("YYYY-MM-DD; defaults to today."),
		}),
		output: coverLetterSchema,
	},
	update: {
		input: revisionSchema.extend(editableSchema.partial().shape).extend({
			aiOperationIds: aiOperationIdsSchema.optional(),
			template: templateSchema.optional().describe("Sets the letter's own template, which unlinks its design."),
			metadata: coverLetterStyleSchema.shape.metadata
				.pick({ typography: true, design: true, page: true })
				.partial()
				.optional()
				.describe("Sets the letter's own type, colors and page settings, which unlinks its design."),
			locale: z
				.string()
				.min(2)
				.max(35)
				.optional()
				.describe("The letter's language for its greeting, sign-off and dates; keeps its design link."),
			recipientName: recipientFieldsSchema.shape.recipientName.unwrap().optional(),
			recipientCompany: recipientFieldsSchema.shape.recipientCompany.unwrap().optional(),
			letterDate: recipientFieldsSchema.shape.letterDate.unwrap().optional(),
			resumeId: z.string().min(1).nullable().optional().describe("The resume the letter goes with."),
			applicationId: z.string().min(1).nullable().optional().describe("The job the letter is for."),
			sender: coverLetterStyleSchema.shape.basics
				.extend({
					name: coverLetterStyleSchema.shape.basics.shape.name.describe("The letter sender's full name."),
					headline: coverLetterStyleSchema.shape.basics.shape.headline.describe("The letter sender's headline."),
					email: coverLetterStyleSchema.shape.basics.shape.email.describe("The letter sender's email address."),
					phone: coverLetterStyleSchema.shape.basics.shape.phone.describe("The letter sender's phone number."),
					location: coverLetterStyleSchema.shape.basics.shape.location.describe("The letter sender's location."),
					website: coverLetterStyleSchema.shape.basics.shape.website.describe("The letter sender's website."),
					customFields: coverLetterStyleSchema.shape.basics.shape.customFields.describe(
						"The letter sender's other contact details and profile links; replaces the whole list.",
					),
					picture: coverLetterStyleSchema.shape.picture
						.pick({ hidden: true })
						.extend({
							hidden: coverLetterStyleSchema.shape.picture.shape.hidden.describe(
								"Whether to hide the copied photo on the letter.",
							),
						})
						.describe("Whether the photo the letter copied from its resume is hidden on the letter."),
				})
				.partial()
				.optional()
				.describe("Edits the letter's own sender details; turn the sender link off first."),
			senderLinked: z.boolean().optional().describe("Take the sender's details live from the resume."),
			designLinked: z.boolean().optional().describe("Take the design live from the resume."),
			proposalApplications: proposalApplicationsSchema,
			proposalSafety: proposalSafetySchema,
			sessionId: z
				.string()
				.min(1)
				.max(64)
				.optional()
				.describe("The editing session; its saves share one History version, refreshed every two minutes."),
		}),
		output: coverLetterSchema,
	},
	listVersions: { input: idSchema.extend(paginationShape), output: z.array(letterVersionSummarySchema) },
	getVersion: { input: versionRefSchema, output: letterVersionSchema },
	createVersion: { input: idSchema.extend({ name: versionNameSchema }), output: letterVersionSummarySchema },
	renameVersion: { input: versionRefSchema.extend({ name: versionNameSchema }), output: letterVersionSummarySchema },
	deleteVersion: { input: versionRefSchema, output: z.void() },
	restoreVersion: {
		input: versionRefSchema.extend({ proposalSafety: proposalSafetySchema }),
		output: coverLetterSchema,
	},
	draft: {
		input: idSchema.extend({
			variant: z
				.enum(["draft", "shorter", "personal"])
				.default("draft")
				.describe("draft writes the body; shorter and personal revise `previous`."),
			previous: z.string().max(20_000).optional().describe("The draft being revised."),
			includeProvenance: z.boolean().optional().describe("Emit an initial AI operation reference before text."),
		}),
	},
	refreshStyle: { input: revisionSchema.extend({ resumeId: z.string().min(1) }), output: coverLetterSchema },
	duplicate: { input: idSchema.extend({ name: editableSchema.shape.name.optional() }), output: coverLetterSchema },
	delete: { input: revisionSchema, output: z.void() },
	export: { input: idSchema, output: coverLetterDocumentSchema },
	import: {
		input: z.object({ document: coverLetterDocumentSchema, aiOperationIds: aiOperationIdsSchema.optional() }),
		output: coverLetterSchema,
	},
};

export type CoverLetterListInput = z.infer<typeof coverLetterDto.list.input>;
export type CoverLetterUpdateInput = z.infer<typeof coverLetterDto.update.input>;
export type CoverLetterDraftInput = z.infer<typeof coverLetterDto.draft.input>;