From 625e468951de2857abf9dc7110021e0437743aa8 Mon Sep 17 00:00:00 2001 From: Knee Cola Date: Tue, 30 Dec 2025 18:29:34 +0100 Subject: [PATCH] refactor: migrate email-worker to use shared-code package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update email-worker to use @evidencija-rezija/shared-code for common types and utilities instead of maintaining duplicate copies. Changes: - Add shared-code dependency to package.json - Update imports in emailSenders.ts to use shared-code - Remove duplicate db-types.ts and shareChecksum.ts files 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- email-worker/package.json | 1 + email-worker/src/lib/emailSenders.ts | 3 +- email-worker/src/lib/shareChecksum.ts | 36 ------------------ email-worker/src/types/db-types.ts | 54 --------------------------- 4 files changed, 2 insertions(+), 92 deletions(-) delete mode 100644 email-worker/src/lib/shareChecksum.ts delete mode 100644 email-worker/src/types/db-types.ts diff --git a/email-worker/package.json b/email-worker/package.json index 21e43d6..6f0f6cc 100644 --- a/email-worker/package.json +++ b/email-worker/package.json @@ -12,6 +12,7 @@ "author": "Nikola", "license": "ISC", "dependencies": { + "@evidencija-rezija/shared-code": "^1.0.0", "debug": "^2.6.9", "express": "^4.18.2", "form-data": "^4.0.5", diff --git a/email-worker/src/lib/emailSenders.ts b/email-worker/src/lib/emailSenders.ts index e1e3792..cf75242 100644 --- a/email-worker/src/lib/emailSenders.ts +++ b/email-worker/src/lib/emailSenders.ts @@ -1,6 +1,5 @@ import { Db, ObjectId } from 'mongodb'; -import { BillingLocation, EmailStatus, UserSettings } from '../types/db-types'; -import { generateShareId } from './shareChecksum'; +import { BillingLocation, EmailStatus, UserSettings, generateShareId } from '@evidencija-rezija/shared-code'; import { sendEmail } from './mailgunService'; import { createLogger } from './logger'; diff --git a/email-worker/src/lib/shareChecksum.ts b/email-worker/src/lib/shareChecksum.ts deleted file mode 100644 index 151b36d..0000000 --- a/email-worker/src/lib/shareChecksum.ts +++ /dev/null @@ -1,36 +0,0 @@ -import crypto from 'crypto'; - -/** - * Checksum length in hex characters (16 chars = 64 bits of entropy) - */ -export const CHECKSUM_LENGTH = 16; - -/** - * Generate share link checksum for location - * Uses HMAC-SHA256 for cryptographic integrity - * - * SECURITY: Prevents location ID enumeration while allowing stateless validation - */ -export function generateShareChecksum(locationId: string): string { - const secret = process.env.SHARE_LINK_SECRET; - - if (!secret) { - throw new Error('SHARE_LINK_SECRET environment variable not configured'); - } - - return crypto - .createHmac('sha256', secret) - .update(locationId) - .digest('hex') - .substring(0, CHECKSUM_LENGTH); -} - -/** - * Generate combined location ID with checksum appended - * @param locationId - The MongoDB location ID (24 chars) - * @returns Combined ID: locationId + checksum (40 chars total) - */ -export function generateShareId(locationId: string): string { - const checksum = generateShareChecksum(locationId); - return locationId + checksum; -} diff --git a/email-worker/src/types/db-types.ts b/email-worker/src/types/db-types.ts deleted file mode 100644 index 48358b4..0000000 --- a/email-worker/src/types/db-types.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { ObjectId } from 'mongodb'; - -export interface YearMonth { - year: number; - month: number; -} - -export enum EmailStatus { - /** Email is not yet verified - recipient has not yet confirmed their email address */ - Unverified = "unverified", - /** Email is not yet verified - a verification request has been sent */ - VerificationPending = "verification-pending", - /** sending of verification email failed */ - VerificationFailed = "verification-failed", - /** Email is verified and is in good standing: emails are being successfully delivered */ - Verified = "verified", - /** Recepient has unsubscribed from receiving emails via link - no further emails will be sent */ - Unsubscribed = "unsubscribed" -} - -/** User settings data */ -export interface UserSettings { - /** user's ID */ - userId: string; - /** owner name */ - ownerName?: string | null; -} - -/** Billing location document from MongoDB */ -export interface BillingLocation { - _id: ObjectId; - /** user's ID */ - userId: string; - /** name of the location */ - name: string; - /** billing period year and month */ - yearMonth: YearMonth; - /** (optional) tenant name */ - tenantName?: string | null; - /** (optional) tenant email */ - tenantEmail?: string | null; - /** (optional) tenant email status */ - tenantEmailStatus?: EmailStatus | null; - /** (optional) whether to automatically notify tenant */ - billFwdEnabled?: boolean | null; - /** (optional) bill forwarding status */ - billFwdStatus?: "pending" | "sent" | "failed" | null; - /** (optional) whether to automatically send rent notification */ - rentDueNotificationEnabled?: boolean | null; - /** (optional) day of month when rent is due (1-31) */ - rentDueDay?: number | null; - /** (optional) when was the rent due notification sent */ - rentDueNotificationStatus?: "sent" | "failed" | null; -}