All files / services/crm settings.service.ts

100% Statements 12/12
100% Branches 14/14
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                          18x     2x 2x   1x   1x               12x 5x 2x         10x 5x 2x           8x      
// CRM Settings Service
import type { Dal } from "../../dal";
import type { CrmSettings } from "../../db/schema";
import { ValidationError } from "../../lib/errors";
 
export type UpdateCrmSettingsInput = {
	kanbanHeaderDisplay?: "count" | "value";
	autoArchiveEnabled?: boolean;
	autoArchiveWonDays?: number;
	autoArchiveLostDays?: number;
};
 
export class CrmSettingsService {
	constructor(private dal: Dal) {}
 
	async getSettings(proId: string): Promise<CrmSettings> {
		const settings = await this.dal.crmSettings.findByProId(proId);
		if (!settings) {
			// Return defaults if not yet created
			return this.dal.crmSettings.upsert(proId, {});
		}
		return settings;
	}
 
	async updateSettings(
		proId: string,
		input: UpdateCrmSettingsInput,
	): Promise<CrmSettings> {
		// Validate archive days if provided
		if (input.autoArchiveWonDays !== undefined) {
			if (input.autoArchiveWonDays < 7 || input.autoArchiveWonDays > 365) {
				throw new ValidationError(
					"Auto-archive won days must be between 7 and 365",
				);
			}
		}
		if (input.autoArchiveLostDays !== undefined) {
			if (input.autoArchiveLostDays < 7 || input.autoArchiveLostDays > 365) {
				throw new ValidationError(
					"Auto-archive lost days must be between 7 and 365",
				);
			}
		}
 
		return this.dal.crmSettings.upsert(proId, input);
	}
}