add shared

This commit is contained in:
Jurgis Sakalauskas
2025-10-14 09:14:01 +03:00
parent 3ac7e9183f
commit b20ab30ece
19 changed files with 134 additions and 211 deletions
+14 -9
View File
@@ -1,6 +1,11 @@
import type { ProjectStatus } from '@shared/models/project';
import { HttpError } from '../errors/http-error.js';
const projectStatuses = new Set(['active', 'archived']);
const projectStatuses = new Set<ProjectStatus>(['active', 'archived']);
const isProjectStatus = (value: unknown): value is ProjectStatus => {
return typeof value === 'string' && projectStatuses.has(value as ProjectStatus);
};
const isStringArray = (value: unknown): value is string[] => {
return (
@@ -12,7 +17,7 @@ const isStringArray = (value: unknown): value is string[] => {
export interface ProjectCreateInput {
name: string;
description: string | null;
status: 'active' | 'archived';
status: ProjectStatus;
projectAttributes: string[];
projectPriorityRules: unknown;
}
@@ -40,7 +45,7 @@ export const parseProjectCreatePayload = (payload: unknown): ProjectCreateInput
throw new HttpError(400, 'description must be a string or null');
}
if (typeof status !== 'string' || !projectStatuses.has(status)) {
if (!isProjectStatus(status)) {
throw new HttpError(400, 'status must be one of active, archived');
}
@@ -52,7 +57,7 @@ export const parseProjectCreatePayload = (payload: unknown): ProjectCreateInput
return {
name: name.trim(),
description: description === null ? null : description,
status: status as 'active' | 'archived',
status: status as ProjectStatus,
projectAttributes,
projectPriorityRules: priorityRules ?? []
};
@@ -84,10 +89,10 @@ export const parseProjectUpdatePayload = (
}
if (status !== undefined) {
if (typeof status !== 'string' || !projectStatuses.has(status)) {
if (!isProjectStatus(status)) {
throw new HttpError(400, 'status must be one of active, archived');
}
result.status = status as 'active' | 'archived';
result.status = status;
}
if (attributes !== undefined) {
@@ -108,12 +113,12 @@ export const parseProjectUpdatePayload = (
return result;
};
export const parseProjectStatusFilter = (value: unknown): 'active' | 'archived' | undefined => {
export const parseProjectStatusFilter = (value: unknown): ProjectStatus | undefined => {
if (value === undefined) {
return undefined;
}
if (typeof value !== 'string' || !projectStatuses.has(value)) {
if (!isProjectStatus(value)) {
throw new HttpError(400, 'status filter must be one of active, archived');
}
return value as 'active' | 'archived';
return value;
};