@lyxa.ai/core 1.1.59 → 1.1.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,7 @@ Perfect for sharing types between frontend and backend applications.
22
22
 
23
23
  ## Version
24
24
 
25
- Version: 1.1.59
25
+ Version: 1.1.61
26
26
 
27
27
  ## Dependencies
28
28
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyxa.ai/types",
3
- "version": "1.1.59",
3
+ "version": "1.1.61",
4
4
  "description": "Lyxa type definitions and validation schemas for both frontend and backend",
5
5
  "author": "elie <42282499+Internalizable@users.noreply.github.com>",
6
6
  "license": "MIT",
@@ -8,25 +8,23 @@ export interface FilterSchemaConfig<TSelect extends ReadonlyNonEmptyArray<string
8
8
  maxSize?: number;
9
9
  defaultSize?: number;
10
10
  }
11
- export type PopulateObject<TPath extends string, TSelectFields extends ReadonlyNonEmptyArray<string>> = {
12
- path: TPath;
13
- select?: Partial<Record<TSelectFields[number], 0 | 1>>;
14
- match?: Record<string, any>;
15
- options?: {
16
- sort?: Record<string, 1 | -1>;
17
- limit?: number;
18
- skip?: number;
11
+ type PopulateMap<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {
12
+ [K in keyof TPopulate]?: {
13
+ select?: Partial<Record<TPopulate[K][number], 0 | 1>>;
14
+ match?: Record<string, any>;
15
+ options?: {
16
+ sort?: Record<string, 1 | -1>;
17
+ limit?: number;
18
+ skip?: number;
19
+ };
19
20
  };
20
21
  };
21
- type PopulateUnion<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {
22
- [K in keyof TPopulate]: PopulateObject<K & string, TPopulate[K]>;
23
- }[keyof TPopulate];
24
22
  export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}> = {
25
23
  size?: number;
26
24
  page?: number;
27
25
  sort?: Record<string, 1 | -1>;
28
26
  select?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;
29
- populate?: keyof TPopulate extends never ? never : PopulateUnion<TPopulate> | PopulateUnion<TPopulate>[];
27
+ populate?: PopulateMap<TPopulate>;
30
28
  search?: TSearch extends ReadonlyNonEmptyArray<string> ? {
31
29
  searchKey: string;
32
30
  searchFields?: TSearch[number][];
@@ -5,7 +5,6 @@ const zod_1 = require("zod");
5
5
  const sortSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.union([zod_1.z.literal(1), zod_1.z.literal(-1)]));
6
6
  function createFilterSchema(config) {
7
7
  const { selectableFields, populatableFields, searchableFields, queryableFields, maxSize = 100, defaultSize = 10, } = config;
8
- const populateKeys = populatableFields ? Object.keys(populatableFields) : [];
9
8
  const schemaShape = {
10
9
  size: zod_1.z.number().min(1).max(maxSize).optional().default(defaultSize),
11
10
  page: zod_1.z.number().min(1).optional().default(1),
@@ -16,12 +15,12 @@ function createFilterSchema(config) {
16
15
  .record(zod_1.z.enum([...selectableFields]), zod_1.z.union([zod_1.z.literal(0), zod_1.z.literal(1)]))
17
16
  .optional();
18
17
  }
19
- if (populateKeys.length) {
20
- const populateSchemas = [];
21
- for (const path of populateKeys) {
18
+ if (populatableFields && Object.keys(populatableFields).length) {
19
+ const populateShape = {};
20
+ for (const path in populatableFields) {
22
21
  const allowedSelects = populatableFields[path];
23
- const populateSchema = zod_1.z.object({
24
- path: zod_1.z.literal(path),
22
+ populateShape[path] = zod_1.z
23
+ .object({
25
24
  select: zod_1.z
26
25
  .record(zod_1.z.enum([...allowedSelects]), zod_1.z.union([zod_1.z.literal(0), zod_1.z.literal(1)]))
27
26
  .optional(),
@@ -33,19 +32,10 @@ function createFilterSchema(config) {
33
32
  skip: zod_1.z.number().optional(),
34
33
  })
35
34
  .optional(),
36
- });
37
- populateSchemas.push(populateSchema);
38
- }
39
- if (populateSchemas.length > 0) {
40
- let unionSchema;
41
- if (populateSchemas.length === 1) {
42
- unionSchema = populateSchemas[0];
43
- }
44
- else {
45
- unionSchema = zod_1.z.union(populateSchemas);
46
- }
47
- schemaShape.populate = zod_1.z.union([unionSchema, zod_1.z.array(unionSchema)]).optional();
35
+ })
36
+ .optional();
48
37
  }
38
+ schemaShape.populate = zod_1.z.object(populateShape).optional();
49
39
  }
50
40
  if (searchableFields?.length) {
51
41
  schemaShape.search = zod_1.z
@@ -1 +1 @@
1
- {"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[],"mappings":";;AAkDA,gDAsFC;AAxID,6BAAoC;AAgDpC,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,KAAK,CAAC,CAAC,OAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEhF,SAAgB,kBAAkB,CAKhC,MAA+D;IAChE,MAAM,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,OAAO,GAAG,GAAG,EACb,WAAW,GAAG,EAAE,GAChB,GAAG,MAAM,CAAC;IAEX,MAAM,YAAY,GAAG,iBAAiB,CAAC,CAAC,CAAE,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAyB,CAAC,CAAC,CAAC,EAAE,CAAC;IAEtG,MAAM,WAAW,GAAwB;QACxC,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC;QACpE,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,CAA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,CAAC,UAAU,CAAC,CAAC,QAAQ,EAAE,CAAC;IACrD,CAAC;IAED,OAAO,OAAC,CAAC,MAAM,CAAC,WAAW,CAA8D,CAAC;AAC3F,CAAC","sourcesContent":["import { z, ZodTypeAny } from 'zod';\n\ntype ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];\n\nexport interface FilterSchemaConfig<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> {\n\tselectableFields?: TSelect;\n\tpopulatableFields?: TPopulate;\n\tsearchableFields?: TSearch;\n\tqueryableFields?: TQuery;\n\tmaxSize?: number;\n\tdefaultSize?: number;\n}\n\n// Each populate path has its own select fields from populatableFields\nexport type PopulateObject<TPath extends string, TSelectFields extends ReadonlyNonEmptyArray<string>> = {\n\tpath: TPath;\n\tselect?: Partial<Record<TSelectFields[number], 0 | 1>>;\n\tmatch?: Record<string, any>;\n\toptions?: { sort?: Record<string, 1 | -1>; limit?: number; skip?: number };\n};\n\n// Create a union type of all possible populate objects based on populatableFields\ntype PopulateUnion<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {\n\t[K in keyof TPopulate]: PopulateObject<K & string, TPopulate[K]>;\n}[keyof TPopulate];\n\nexport type FilterDTO<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> = {\n\tsize?: number;\n\tpage?: number;\n\tsort?: Record<string, 1 | -1>;\n\tselect?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;\n\tpopulate?: keyof TPopulate extends never ? never : PopulateUnion<TPopulate> | PopulateUnion<TPopulate>[];\n\tsearch?: TSearch extends ReadonlyNonEmptyArray<string>\n\t\t? { searchKey: string; searchFields?: TSearch[number][] }\n\t\t: never;\n\tquery?: { [K in keyof TQuery]?: z.infer<TQuery[K]> };\n};\n\nconst sortSchema = z.record(z.string(), z.union([z.literal(1), z.literal(-1)]));\n\nexport function createFilterSchema<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery>) {\n\tconst {\n\t\tselectableFields,\n\t\tpopulatableFields,\n\t\tsearchableFields,\n\t\tqueryableFields,\n\t\tmaxSize = 100,\n\t\tdefaultSize = 10,\n\t} = config;\n\n\tconst populateKeys = populatableFields ? (Object.keys(populatableFields) as (keyof TPopulate)[]) : [];\n\n\tconst schemaShape: Record<string, any> = {\n\t\tsize: z.number().min(1).max(maxSize).optional().default(defaultSize),\n\t\tpage: z.number().min(1).optional().default(1),\n\t\tsort: sortSchema.optional(),\n\t};\n\n\t// Select - uses selectableFields for top-level document fields\n\tif (selectableFields?.length) {\n\t\tschemaShape.select = z\n\t\t\t.record(z.enum([...selectableFields] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t.optional();\n\t}\n\n\t// Populate - each path uses its own select fields from populatableFields\n\tif (populateKeys.length) {\n\t\tconst populateSchemas: ZodTypeAny[] = [];\n\n\t\tfor (const path of populateKeys) {\n\t\t\tconst allowedSelects = populatableFields![path] as ReadonlyNonEmptyArray<string>;\n\n\t\t\tconst populateSchema = z.object({\n\t\t\t\tpath: z.literal(path as string),\n\t\t\t\tselect: z\n\t\t\t\t\t.record(z.enum([...allowedSelects] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t\t\t.optional(),\n\t\t\t\tmatch: z.record(z.string(), z.any()).optional(),\n\t\t\t\toptions: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tsort: sortSchema.optional(),\n\t\t\t\t\t\tlimit: z.number().optional(),\n\t\t\t\t\t\tskip: z.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t});\n\n\t\t\tpopulateSchemas.push(populateSchema);\n\t\t}\n\n\t\tif (populateSchemas.length > 0) {\n\t\t\tlet unionSchema: ZodTypeAny;\n\t\t\tif (populateSchemas.length === 1) {\n\t\t\t\tunionSchema = populateSchemas[0];\n\t\t\t} else {\n\t\t\t\tunionSchema = z.union(populateSchemas as [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]);\n\t\t\t}\n\t\t\tschemaShape.populate = z.union([unionSchema, z.array(unionSchema)]).optional();\n\t\t}\n\t}\n\n\t// Search\n\tif (searchableFields?.length) {\n\t\tschemaShape.search = z\n\t\t\t.object({\n\t\t\t\tsearchKey: z.string(),\n\t\t\t\tsearchFields: z.array(z.enum([...searchableFields] as [string, ...string[]])).optional(),\n\t\t\t})\n\t\t\t.optional();\n\t}\n\n\t// Query\n\tif (queryableFields && Object.keys(queryableFields).length) {\n\t\tconst queryShape: Record<string, ZodTypeAny> = {};\n\t\tfor (const key in queryableFields) {\n\t\t\tqueryShape[key] = queryableFields[key].optional();\n\t\t}\n\t\tschemaShape.query = z.object(queryShape).optional();\n\t}\n\n\treturn z.object(schemaShape) as z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;\n}\n\n/**\n * EXAMPLE USAGE\n *\n * const TicketFilterSchema = createFilterSchema({\n * // Top-level document fields that can be selected\n * selectableFields: [\n * '_id',\n * 'ticketId',\n * 'type',\n * 'status',\n * 'createdAt',\n * 'updatedAt',\n * ] as const,\n *\n * // Each populate path has its own allowed select fields\n * populatableFields: {\n * chatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n * client: ['_id', 'name', 'profilePhoto', 'type'] as const,\n * admin: ['_id', 'name', 'profilePhoto'] as const,\n * order: ['_id', 'orderId', 'shop', 'total'] as const,\n * } as const,\n *\n * searchableFields: ['ticketId'] as const,\n *\n * queryableFields: {\n * status: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n * type: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n * client: z.string(),\n * clientType: z.enum(['USER', 'SHOP']),\n * },\n * });\n *\n * type TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n *\n * // ✅ VALID EXAMPLES\n *\n * // Example 1: Select only top-level fields\n * const filter1: TicketFilterDTO = {\n * select: { ticketId: 1, status: 1, createdAt: 1 },\n * query: { status: 'OPEN' },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 2: Populate client with specific fields\n * const filter2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { name: 1, profilePhoto: 1 }, // Uses client's allowed fields\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 3: Populate multiple relations\n * const filter3: TicketFilterDTO = {\n * populate: [\n * { path: 'client', select: { name: 1, profilePhoto: 1 } },\n * { path: 'order', select: { orderId: 1, total: 1 } },\n * { path: 'chatroom', select: { lastMessage: 1, unreadCount: 1 } },\n * ],\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 4: Complex query with search\n * const filter4: TicketFilterDTO = {\n * query: { status: 'OPEN', type: 'ORDER' },\n * search: { searchKey: 'TKT-123', searchFields: ['ticketId'] },\n * populate: { path: 'client', select: { name: 1 } },\n * sort: { createdAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 5: Populate with match and options\n * const filter5: TicketFilterDTO = {\n * populate: {\n * path: 'order',\n * select: { orderId: 1, shop: 1 },\n * match: { status: 'DELIVERED' },\n * options: { sort: { createdAt: -1 }, limit: 10 },\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // ❌ INVALID EXAMPLES (TypeScript will reject)\n *\n * const invalid1: TicketFilterDTO = {\n * select: { invalidField: 1 }, // ❌ not in selectableFields\n * };\n *\n * const invalid2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { email: 1 }, // ❌ email not in client's populatableFields\n * },\n * };\n *\n * const invalid3: TicketFilterDTO = {\n * populate: {\n * path: 'invalidPath', // ❌ path doesn't exist\n * select: { name: 1 },\n * },\n * };\n *\n * const invalid4: TicketFilterDTO = {\n * query: { status: 'INVALID_STATUS' }, // ❌ not in enum\n * }\n */"]}
1
+ {"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[],"mappings":";;AAgDA,gDA8EC;AA9HD,6BAAoC;AA6CpC,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,KAAK,CAAC,CAAC,OAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAGhF,SAAgB,kBAAkB,CAKhC,MAA+D;IAChE,MAAM,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EACf,OAAO,GAAG,GAAG,EACb,WAAW,GAAG,EAAE,GAChB,GAAG,MAAM,CAAC;IAEX,MAAM,WAAW,GAAwB;QACxC,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC;QACpE,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7C,IAAI,EAAE,UAAU,CAAC,QAAQ,EAAE;KAC3B,CAAC;IAGF,IAAI,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAC9B,WAAW,CAAC,MAAM,GAAG,OAAC;aACpB,MAAM,CAAC,OAAC,CAAC,IAAI,CAAC,CAAC,GAAG,gBAAg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AE,CAAC;IAC3D,CAAC;IAGD,IAAI,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAC9B,WAAW,CAAC,MAAM,GAAG,OAAC;aACpB,MAAM,CAAC;YACP,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE;YACrB,YAAY,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,IAAI,CAAC,CAAC,GAAG,gBAAgB,CAA0B,CAAC,CAAC,CAAC,QAAQ,EAAE;SACxF,CAAC;aACD,QAAQ,EAAE,CAAC;IACd,CAAC;IAGD,IAAI,eAAe,IAAI,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,EAAE,CAAC;QAC5D,MAAM,UAAU,GAA+B,EAAE,CAAC;QAClD,KAAK,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;YACnC,UAAU,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;QACnD,CAAC;QACD,WAAW,CAAC,KAAK,GAAG,OAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,EAAE,CAAC;IACrD,CAAC;IAED,OAAO,OAAC,CAAC,MAAM,CAAC,WAAW,CAA8D,CAAC;AAC3F,CAAC","sourcesContent":["import { z, ZodTypeAny } from 'zod';\n\ntype ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];\n\nexport interface FilterSchemaConfig<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> {\n\tselectableFields?: TSelect;\n\tpopulatableFields?: TPopulate;\n\tsearchableFields?: TSearch;\n\tqueryableFields?: TQuery;\n\tmaxSize?: number;\n\tdefaultSize?: number;\n}\n\n// Mapped type for keyed populate\ntype PopulateMap<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {\n\t[K in keyof TPopulate]?: {\n\t\tselect?: Partial<Record<TPopulate[K][number], 0 | 1>>;\n\t\tmatch?: Record<string, any>;\n\t\toptions?: { sort?: Record<string, 1 | -1>; limit?: number; skip?: number };\n\t};\n};\n\n// Filter DTO\nexport type FilterDTO<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> = {\n\tsize?: number;\n\tpage?: number;\n\tsort?: Record<string, 1 | -1>;\n\tselect?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;\n\tpopulate?: PopulateMap<TPopulate>;\n\tsearch?: TSearch extends ReadonlyNonEmptyArray<string>\n\t\t? { searchKey: string; searchFields?: TSearch[number][] }\n\t\t: never;\n\tquery?: { [K in keyof TQuery]?: z.infer<TQuery[K]> };\n};\n\nconst sortSchema = z.record(z.string(), z.union([z.literal(1), z.literal(-1)]));\n\n// Create the Zod schema\nexport function createFilterSchema<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery>) {\n\tconst {\n\t\tselectableFields,\n\t\tpopulatableFields,\n\t\tsearchableFields,\n\t\tqueryableFields,\n\t\tmaxSize = 100,\n\t\tdefaultSize = 10,\n\t} = config;\n\n\tconst schemaShape: Record<string, any> = {\n\t\tsize: z.number().min(1).max(maxSize).optional().default(defaultSize),\n\t\tpage: z.number().min(1).optional().default(1),\n\t\tsort: sortSchema.optional(),\n\t};\n\n\t// Top-level select\n\tif (selectableFields?.length) {\n\t\tschemaShape.select = z\n\t\t\t.record(z.enum([...selectableFields] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t.optional();\n\t}\n\n\t// Keyed populate\n\tif (populatableFields && Object.keys(populatableFields).length) {\n\t\tconst populateShape: Record<string, any> = {};\n\n\t\tfor (const path in populatableFields) {\n\t\t\tconst allowedSelects = populatableFields[path] as ReadonlyNonEmptyArray<string>;\n\n\t\t\tpopulateShape[path] = z\n\t\t\t\t.object({\n\t\t\t\t\tselect: z\n\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\tz.enum([...allowedSelects] as [string, ...string[]]),\n\t\t\t\t\t\t\tz.union([z.literal(0), z.literal(1)])\n\t\t\t\t\t\t)\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tmatch: z.record(z.string(), z.any()).optional(),\n\t\t\t\t\toptions: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\tsort: sortSchema.optional(),\n\t\t\t\t\t\t\tlimit: z.number().optional(),\n\t\t\t\t\t\t\tskip: z.number().optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t})\n\t\t\t\t.optional();\n\t\t}\n\n\t\tschemaShape.populate = z.object(populateShape).optional();\n\t}\n\n\t// Search\n\tif (searchableFields?.length) {\n\t\tschemaShape.search = z\n\t\t\t.object({\n\t\t\t\tsearchKey: z.string(),\n\t\t\t\tsearchFields: z.array(z.enum([...searchableFields] as [string, ...string[]])).optional(),\n\t\t\t})\n\t\t\t.optional();\n\t}\n\n\t// Query\n\tif (queryableFields && Object.keys(queryableFields).length) {\n\t\tconst queryShape: Record<string, ZodTypeAny> = {};\n\t\tfor (const key in queryableFields) {\n\t\t\tqueryShape[key] = queryableFields[key].optional();\n\t\t}\n\t\tschemaShape.query = z.object(queryShape).optional();\n\t}\n\n\treturn z.object(schemaShape) as z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;\n}\n\n/**\n * EXAMPLE USAGE\n *\n * const TicketFilterSchema = createFilterSchema({\n * // Top-level document fields that can be selected\n * selectableFields: [\n * '_id',\n * 'ticketId',\n * 'type',\n * 'status',\n * 'createdAt',\n * 'updatedAt',\n * ] as const,\n *\n * // Each populate path has its own allowed select fields\n * populatableFields: {\n * chatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n * client: ['_id', 'name', 'profilePhoto', 'type'] as const,\n * admin: ['_id', 'name', 'profilePhoto'] as const,\n * order: ['_id', 'orderId', 'shop', 'total'] as const,\n * } as const,\n *\n * searchableFields: ['ticketId'] as const,\n *\n * queryableFields: {\n * status: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n * type: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n * client: z.string(),\n * clientType: z.enum(['USER', 'SHOP']),\n * },\n * });\n *\n * type TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n *\n * // ✅ VALID EXAMPLES\n *\n * // Example 1: Select only top-level fields\n * const filter1: TicketFilterDTO = {\n * select: { ticketId: 1, status: 1, createdAt: 1 },\n * query: { status: 'OPEN' },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 2: Populate client with specific fields\n * const filter2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { name: 1, profilePhoto: 1 }, // Uses client's allowed fields\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 3: Populate multiple relations\n * const filter3: TicketFilterDTO = {\n * populate: [\n * { path: 'client', select: { name: 1, profilePhoto: 1 } },\n * { path: 'order', select: { orderId: 1, total: 1 } },\n * { path: 'chatroom', select: { lastMessage: 1, unreadCount: 1 } },\n * ],\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 4: Complex query with search\n * const filter4: TicketFilterDTO = {\n * query: { status: 'OPEN', type: 'ORDER' },\n * search: { searchKey: 'TKT-123', searchFields: ['ticketId'] },\n * populate: { path: 'client', select: { name: 1 } },\n * sort: { createdAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 5: Populate with match and options\n * const filter5: TicketFilterDTO = {\n * populate: {\n * path: 'order',\n * select: { orderId: 1, shop: 1 },\n * match: { status: 'DELIVERED' },\n * options: { sort: { createdAt: -1 }, limit: 10 },\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // ❌ INVALID EXAMPLES (TypeScript will reject)\n *\n * const invalid1: TicketFilterDTO = {\n * select: { invalidField: 1 }, // ❌ not in selectableFields\n * };\n *\n * const invalid2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { email: 1 }, // ❌ email not in client's populatableFields\n * },\n * };\n *\n * const invalid3: TicketFilterDTO = {\n * populate: {\n * path: 'invalidPath', // ❌ path doesn't exist\n * select: { name: 1 },\n * },\n * };\n *\n * const invalid4: TicketFilterDTO = {\n * query: { status: 'INVALID_STATUS' }, // ❌ not in enum\n * }\n */"]}
@@ -1,4 +1,4 @@
1
1
  export * from './common-validation';
2
2
  export * from './global-validation';
3
3
  export * from './validation-for-frontend';
4
- export { createFilterSchema, FilterSchemaConfig, PopulateObject } from './filter-schema.factory';
4
+ export { createFilterSchema, FilterSchemaConfig } from './filter-schema.factory';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["utilities/validation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,sDAAoC;AACpC,4DAA0C;AAC1C,iEAAiG;AAAxF,2HAAA,kBAAkB,OAAA","sourcesContent":["export * from './common-validation';\nexport * from './global-validation';\nexport * from './validation-for-frontend';\nexport { createFilterSchema, FilterSchemaConfig, PopulateObject } from './filter-schema.factory';"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["utilities/validation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,sDAAoC;AACpC,4DAA0C;AAC1C,iEAAiF;AAAxE,2HAAA,kBAAkB,OAAA","sourcesContent":["export * from './common-validation';\nexport * from './global-validation';\nexport * from './validation-for-frontend';\nexport { createFilterSchema, FilterSchemaConfig } from './filter-schema.factory';"]}
@@ -8,25 +8,23 @@ export interface FilterSchemaConfig<TSelect extends ReadonlyNonEmptyArray<string
8
8
  maxSize?: number;
9
9
  defaultSize?: number;
10
10
  }
11
- export type PopulateObject<TPath extends string, TSelectFields extends ReadonlyNonEmptyArray<string>> = {
12
- path: TPath;
13
- select?: Partial<Record<TSelectFields[number], 0 | 1>>;
14
- match?: Record<string, any>;
15
- options?: {
16
- sort?: Record<string, 1 | -1>;
17
- limit?: number;
18
- skip?: number;
11
+ type PopulateMap<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {
12
+ [K in keyof TPopulate]?: {
13
+ select?: Partial<Record<TPopulate[K][number], 0 | 1>>;
14
+ match?: Record<string, any>;
15
+ options?: {
16
+ sort?: Record<string, 1 | -1>;
17
+ limit?: number;
18
+ skip?: number;
19
+ };
19
20
  };
20
21
  };
21
- type PopulateUnion<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {
22
- [K in keyof TPopulate]: PopulateObject<K & string, TPopulate[K]>;
23
- }[keyof TPopulate];
24
22
  export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}> = {
25
23
  size?: number;
26
24
  page?: number;
27
25
  sort?: Record<string, 1 | -1>;
28
26
  select?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;
29
- populate?: keyof TPopulate extends never ? never : PopulateUnion<TPopulate> | PopulateUnion<TPopulate>[];
27
+ populate?: PopulateMap<TPopulate>;
30
28
  search?: TSearch extends ReadonlyNonEmptyArray<string> ? {
31
29
  searchKey: string;
32
30
  searchFields?: TSearch[number][];
@@ -5,7 +5,6 @@ const zod_1 = require("zod");
5
5
  const sortSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.union([zod_1.z.literal(1), zod_1.z.literal(-1)]));
6
6
  function createFilterSchema(config) {
7
7
  const { selectableFields, populatableFields, searchableFields, queryableFields, maxSize = 100, defaultSize = 10, } = config;
8
- const populateKeys = populatableFields ? Object.keys(populatableFields) : [];
9
8
  const schemaShape = {
10
9
  size: zod_1.z.number().min(1).max(maxSize).optional().default(defaultSize),
11
10
  page: zod_1.z.number().min(1).optional().default(1),
@@ -16,12 +15,12 @@ function createFilterSchema(config) {
16
15
  .record(zod_1.z.enum([...selectableFields]), zod_1.z.union([zod_1.z.literal(0), zod_1.z.literal(1)]))
17
16
  .optional();
18
17
  }
19
- if (populateKeys.length) {
20
- const populateSchemas = [];
21
- for (const path of populateKeys) {
18
+ if (populatableFields && Object.keys(populatableFields).length) {
19
+ const populateShape = {};
20
+ for (const path in populatableFields) {
22
21
  const allowedSelects = populatableFields[path];
23
- const populateSchema = zod_1.z.object({
24
- path: zod_1.z.literal(path),
22
+ populateShape[path] = zod_1.z
23
+ .object({
25
24
  select: zod_1.z
26
25
  .record(zod_1.z.enum([...allowedSelects]), zod_1.z.union([zod_1.z.literal(0), zod_1.z.literal(1)]))
27
26
  .optional(),
@@ -33,19 +32,10 @@ function createFilterSchema(config) {
33
32
  skip: zod_1.z.number().optional(),
34
33
  })
35
34
  .optional(),
36
- });
37
- populateSchemas.push(populateSchema);
38
- }
39
- if (populateSchemas.length > 0) {
40
- let unionSchema;
41
- if (populateSchemas.length === 1) {
42
- unionSchema = populateSchemas[0];
43
- }
44
- else {
45
- unionSchema = zod_1.z.union(populateSchemas);
46
- }
47
- schemaShape.populate = zod_1.z.union([unionSchema, zod_1.z.array(unionSchema)]).optional();
35
+ })
36
+ .optional();
48
37
  }
38
+ schemaShape.populate = zod_1.z.object(populateShape).optional();
49
39
  }
50
40
  if (searchableFields?.length) {
51
41
  schemaShape.search = zod_1.z
@@ -1 +1 @@
1
- {"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[],"mappings":";;AAkDA,gDAsFC;AAxID,6BAAoC;AAgDpC,MAAM,UAAU,GA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C,QAAQ,GAAG,OAAC,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;QAChF,CAAC;IACF,CAAC;IAGD,IAAI,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAC9B,WAAW,CAAC,MAAM,GAAG,OAAC;aACpB,MAAM,CAAC;YACP,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE;YACrB,YAAY,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,IAAI,CAAC,CAAC,GAAG,gBAAgB,CAA0B,CAAC,CAAC,CAAC,QAAQ,EAAE;SACxF,CAAC;aACD,QAAQ,EAAE,CAAC;IACd,CAAC;IAGD,IAAI,eAAe,IAAI,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,EAAE,CAAC;QAC5D,MAAM,UAAU,GAA+B,EAAE,CAAC;QAClD,KAAK,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;YACnC,UAAU,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;QACnD,CAAC;QACD,WAAW,CAAC,KAAK,GAAG,OAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,EAAE,CAAC;IACrD,CAAC;IAED,OAAO,OAAC,CAAC,MAAM,CAAC,WAAW,CAA8D,CAAC;AAC3F,CAAC","sourcesContent":["import { z, ZodTypeAny } from 'zod';\n\ntype ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];\n\nexport interface FilterSchemaConfig<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> {\n\tselectableFields?: TSelect;\n\tpopulatableFields?: TPopulate;\n\tsearchableFields?: TSearch;\n\tqueryableFields?: TQuery;\n\tmaxSize?: number;\n\tdefaultSize?: number;\n}\n\n// Each populate path has its own select fields from populatableFields\nexport type PopulateObject<TPath extends string, TSelectFields extends ReadonlyNonEmptyArray<string>> = {\n\tpath: TPath;\n\tselect?: Partial<Record<TSelectFields[number], 0 | 1>>;\n\tmatch?: Record<string, any>;\n\toptions?: { sort?: Record<string, 1 | -1>; limit?: number; skip?: number };\n};\n\n// Create a union type of all possible populate objects based on populatableFields\ntype PopulateUnion<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {\n\t[K in keyof TPopulate]: PopulateObject<K & string, TPopulate[K]>;\n}[keyof TPopulate];\n\nexport type FilterDTO<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> = {\n\tsize?: number;\n\tpage?: number;\n\tsort?: Record<string, 1 | -1>;\n\tselect?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;\n\tpopulate?: keyof TPopulate extends never ? never : PopulateUnion<TPopulate> | PopulateUnion<TPopulate>[];\n\tsearch?: TSearch extends ReadonlyNonEmptyArray<string>\n\t\t? { searchKey: string; searchFields?: TSearch[number][] }\n\t\t: never;\n\tquery?: { [K in keyof TQuery]?: z.infer<TQuery[K]> };\n};\n\nconst sortSchema = z.record(z.string(), z.union([z.literal(1), z.literal(-1)]));\n\nexport function createFilterSchema<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery>) {\n\tconst {\n\t\tselectableFields,\n\t\tpopulatableFields,\n\t\tsearchableFields,\n\t\tqueryableFields,\n\t\tmaxSize = 100,\n\t\tdefaultSize = 10,\n\t} = config;\n\n\tconst populateKeys = populatableFields ? (Object.keys(populatableFields) as (keyof TPopulate)[]) : [];\n\n\tconst schemaShape: Record<string, any> = {\n\t\tsize: z.number().min(1).max(maxSize).optional().default(defaultSize),\n\t\tpage: z.number().min(1).optional().default(1),\n\t\tsort: sortSchema.optional(),\n\t};\n\n\t// Select - uses selectableFields for top-level document fields\n\tif (selectableFields?.length) {\n\t\tschemaShape.select = z\n\t\t\t.record(z.enum([...selectableFields] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t.optional();\n\t}\n\n\t// Populate - each path uses its own select fields from populatableFields\n\tif (populateKeys.length) {\n\t\tconst populateSchemas: ZodTypeAny[] = [];\n\n\t\tfor (const path of populateKeys) {\n\t\t\tconst allowedSelects = populatableFields![path] as ReadonlyNonEmptyArray<string>;\n\n\t\t\tconst populateSchema = z.object({\n\t\t\t\tpath: z.literal(path as string),\n\t\t\t\tselect: z\n\t\t\t\t\t.record(z.enum([...allowedSelects] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t\t\t.optional(),\n\t\t\t\tmatch: z.record(z.string(), z.any()).optional(),\n\t\t\t\toptions: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tsort: sortSchema.optional(),\n\t\t\t\t\t\tlimit: z.number().optional(),\n\t\t\t\t\t\tskip: z.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t});\n\n\t\t\tpopulateSchemas.push(populateSchema);\n\t\t}\n\n\t\tif (populateSchemas.length > 0) {\n\t\t\tlet unionSchema: ZodTypeAny;\n\t\t\tif (populateSchemas.length === 1) {\n\t\t\t\tunionSchema = populateSchemas[0];\n\t\t\t} else {\n\t\t\t\tunionSchema = z.union(populateSchemas as [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]);\n\t\t\t}\n\t\t\tschemaShape.populate = z.union([unionSchema, z.array(unionSchema)]).optional();\n\t\t}\n\t}\n\n\t// Search\n\tif (searchableFields?.length) {\n\t\tschemaShape.search = z\n\t\t\t.object({\n\t\t\t\tsearchKey: z.string(),\n\t\t\t\tsearchFields: z.array(z.enum([...searchableFields] as [string, ...string[]])).optional(),\n\t\t\t})\n\t\t\t.optional();\n\t}\n\n\t// Query\n\tif (queryableFields && Object.keys(queryableFields).length) {\n\t\tconst queryShape: Record<string, ZodTypeAny> = {};\n\t\tfor (const key in queryableFields) {\n\t\t\tqueryShape[key] = queryableFields[key].optional();\n\t\t}\n\t\tschemaShape.query = z.object(queryShape).optional();\n\t}\n\n\treturn z.object(schemaShape) as z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;\n}\n\n/**\n * EXAMPLE USAGE\n *\n * const TicketFilterSchema = createFilterSchema({\n * // Top-level document fields that can be selected\n * selectableFields: [\n * '_id',\n * 'ticketId',\n * 'type',\n * 'status',\n * 'createdAt',\n * 'updatedAt',\n * ] as const,\n *\n * // Each populate path has its own allowed select fields\n * populatableFields: {\n * chatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n * client: ['_id', 'name', 'profilePhoto', 'type'] as const,\n * admin: ['_id', 'name', 'profilePhoto'] as const,\n * order: ['_id', 'orderId', 'shop', 'total'] as const,\n * } as const,\n *\n * searchableFields: ['ticketId'] as const,\n *\n * queryableFields: {\n * status: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n * type: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n * client: z.string(),\n * clientType: z.enum(['USER', 'SHOP']),\n * },\n * });\n *\n * type TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n *\n * // ✅ VALID EXAMPLES\n *\n * // Example 1: Select only top-level fields\n * const filter1: TicketFilterDTO = {\n * select: { ticketId: 1, status: 1, createdAt: 1 },\n * query: { status: 'OPEN' },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 2: Populate client with specific fields\n * const filter2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { name: 1, profilePhoto: 1 }, // Uses client's allowed fields\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 3: Populate multiple relations\n * const filter3: TicketFilterDTO = {\n * populate: [\n * { path: 'client', select: { name: 1, profilePhoto: 1 } },\n * { path: 'order', select: { orderId: 1, total: 1 } },\n * { path: 'chatroom', select: { lastMessage: 1, unreadCount: 1 } },\n * ],\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 4: Complex query with search\n * const filter4: TicketFilterDTO = {\n * query: { status: 'OPEN', type: 'ORDER' },\n * search: { searchKey: 'TKT-123', searchFields: ['ticketId'] },\n * populate: { path: 'client', select: { name: 1 } },\n * sort: { createdAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 5: Populate with match and options\n * const filter5: TicketFilterDTO = {\n * populate: {\n * path: 'order',\n * select: { orderId: 1, shop: 1 },\n * match: { status: 'DELIVERED' },\n * options: { sort: { createdAt: -1 }, limit: 10 },\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // ❌ INVALID EXAMPLES (TypeScript will reject)\n *\n * const invalid1: TicketFilterDTO = {\n * select: { invalidField: 1 }, // ❌ not in selectableFields\n * };\n *\n * const invalid2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { email: 1 }, // ❌ email not in client's populatableFields\n * },\n * };\n *\n * const invalid3: TicketFilterDTO = {\n * populate: {\n * path: 'invalidPath', // ❌ path doesn't exist\n * select: { name: 1 },\n * },\n * };\n *\n * const invalid4: TicketFilterDTO = {\n * query: { status: 'INVALID_STATUS' }, // ❌ not in enum\n * }\n */"]}
1
+ {"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[],"mappings":";;AAgDA,gDA8EC;AA9HD,6BAAoC;AA6CpC,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,KAAK,CAAC,CAAC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,CAAC;QAClD,KAAK,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;YACnC,UAAU,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;QACnD,CAAC;QACD,WAAW,CAAC,KAAK,GAAG,OAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,EAAE,CAAC;IACrD,CAAC;IAED,OAAO,OAAC,CAAC,MAAM,CAAC,WAAW,CAA8D,CAAC;AAC3F,CAAC","sourcesContent":["import { z, ZodTypeAny } from 'zod';\n\ntype ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];\n\nexport interface FilterSchemaConfig<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> {\n\tselectableFields?: TSelect;\n\tpopulatableFields?: TPopulate;\n\tsearchableFields?: TSearch;\n\tqueryableFields?: TQuery;\n\tmaxSize?: number;\n\tdefaultSize?: number;\n}\n\n// Mapped type for keyed populate\ntype PopulateMap<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {\n\t[K in keyof TPopulate]?: {\n\t\tselect?: Partial<Record<TPopulate[K][number], 0 | 1>>;\n\t\tmatch?: Record<string, any>;\n\t\toptions?: { sort?: Record<string, 1 | -1>; limit?: number; skip?: number };\n\t};\n};\n\n// Filter DTO\nexport type FilterDTO<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> = {\n\tsize?: number;\n\tpage?: number;\n\tsort?: Record<string, 1 | -1>;\n\tselect?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;\n\tpopulate?: PopulateMap<TPopulate>;\n\tsearch?: TSearch extends ReadonlyNonEmptyArray<string>\n\t\t? { searchKey: string; searchFields?: TSearch[number][] }\n\t\t: never;\n\tquery?: { [K in keyof TQuery]?: z.infer<TQuery[K]> };\n};\n\nconst sortSchema = z.record(z.string(), z.union([z.literal(1), z.literal(-1)]));\n\n// Create the Zod schema\nexport function createFilterSchema<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery>) {\n\tconst {\n\t\tselectableFields,\n\t\tpopulatableFields,\n\t\tsearchableFields,\n\t\tqueryableFields,\n\t\tmaxSize = 100,\n\t\tdefaultSize = 10,\n\t} = config;\n\n\tconst schemaShape: Record<string, any> = {\n\t\tsize: z.number().min(1).max(maxSize).optional().default(defaultSize),\n\t\tpage: z.number().min(1).optional().default(1),\n\t\tsort: sortSchema.optional(),\n\t};\n\n\t// Top-level select\n\tif (selectableFields?.length) {\n\t\tschemaShape.select = z\n\t\t\t.record(z.enum([...selectableFields] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t.optional();\n\t}\n\n\t// Keyed populate\n\tif (populatableFields && Object.keys(populatableFields).length) {\n\t\tconst populateShape: Record<string, any> = {};\n\n\t\tfor (const path in populatableFields) {\n\t\t\tconst allowedSelects = populatableFields[path] as ReadonlyNonEmptyArray<string>;\n\n\t\t\tpopulateShape[path] = z\n\t\t\t\t.object({\n\t\t\t\t\tselect: z\n\t\t\t\t\t\t.record(\n\t\t\t\t\t\t\tz.enum([...allowedSelects] as [string, ...string[]]),\n\t\t\t\t\t\t\tz.union([z.literal(0), z.literal(1)])\n\t\t\t\t\t\t)\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tmatch: z.record(z.string(), z.any()).optional(),\n\t\t\t\t\toptions: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\tsort: sortSchema.optional(),\n\t\t\t\t\t\t\tlimit: z.number().optional(),\n\t\t\t\t\t\t\tskip: z.number().optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t})\n\t\t\t\t.optional();\n\t\t}\n\n\t\tschemaShape.populate = z.object(populateShape).optional();\n\t}\n\n\t// Search\n\tif (searchableFields?.length) {\n\t\tschemaShape.search = z\n\t\t\t.object({\n\t\t\t\tsearchKey: z.string(),\n\t\t\t\tsearchFields: z.array(z.enum([...searchableFields] as [string, ...string[]])).optional(),\n\t\t\t})\n\t\t\t.optional();\n\t}\n\n\t// Query\n\tif (queryableFields && Object.keys(queryableFields).length) {\n\t\tconst queryShape: Record<string, ZodTypeAny> = {};\n\t\tfor (const key in queryableFields) {\n\t\t\tqueryShape[key] = queryableFields[key].optional();\n\t\t}\n\t\tschemaShape.query = z.object(queryShape).optional();\n\t}\n\n\treturn z.object(schemaShape) as z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;\n}\n\n/**\n * EXAMPLE USAGE\n *\n * const TicketFilterSchema = createFilterSchema({\n * // Top-level document fields that can be selected\n * selectableFields: [\n * '_id',\n * 'ticketId',\n * 'type',\n * 'status',\n * 'createdAt',\n * 'updatedAt',\n * ] as const,\n *\n * // Each populate path has its own allowed select fields\n * populatableFields: {\n * chatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n * client: ['_id', 'name', 'profilePhoto', 'type'] as const,\n * admin: ['_id', 'name', 'profilePhoto'] as const,\n * order: ['_id', 'orderId', 'shop', 'total'] as const,\n * } as const,\n *\n * searchableFields: ['ticketId'] as const,\n *\n * queryableFields: {\n * status: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n * type: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n * client: z.string(),\n * clientType: z.enum(['USER', 'SHOP']),\n * },\n * });\n *\n * type TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n *\n * // ✅ VALID EXAMPLES\n *\n * // Example 1: Select only top-level fields\n * const filter1: TicketFilterDTO = {\n * select: { ticketId: 1, status: 1, createdAt: 1 },\n * query: { status: 'OPEN' },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 2: Populate client with specific fields\n * const filter2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { name: 1, profilePhoto: 1 }, // Uses client's allowed fields\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 3: Populate multiple relations\n * const filter3: TicketFilterDTO = {\n * populate: [\n * { path: 'client', select: { name: 1, profilePhoto: 1 } },\n * { path: 'order', select: { orderId: 1, total: 1 } },\n * { path: 'chatroom', select: { lastMessage: 1, unreadCount: 1 } },\n * ],\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 4: Complex query with search\n * const filter4: TicketFilterDTO = {\n * query: { status: 'OPEN', type: 'ORDER' },\n * search: { searchKey: 'TKT-123', searchFields: ['ticketId'] },\n * populate: { path: 'client', select: { name: 1 } },\n * sort: { createdAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 5: Populate with match and options\n * const filter5: TicketFilterDTO = {\n * populate: {\n * path: 'order',\n * select: { orderId: 1, shop: 1 },\n * match: { status: 'DELIVERED' },\n * options: { sort: { createdAt: -1 }, limit: 10 },\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // ❌ INVALID EXAMPLES (TypeScript will reject)\n *\n * const invalid1: TicketFilterDTO = {\n * select: { invalidField: 1 }, // ❌ not in selectableFields\n * };\n *\n * const invalid2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { email: 1 }, // ❌ email not in client's populatableFields\n * },\n * };\n *\n * const invalid3: TicketFilterDTO = {\n * populate: {\n * path: 'invalidPath', // ❌ path doesn't exist\n * select: { name: 1 },\n * },\n * };\n *\n * const invalid4: TicketFilterDTO = {\n * query: { status: 'INVALID_STATUS' }, // ❌ not in enum\n * }\n */"]}
@@ -1,4 +1,4 @@
1
1
  export * from './common-validation';
2
2
  export * from './global-validation';
3
3
  export * from './validation-for-frontend';
4
- export { createFilterSchema, FilterSchemaConfig, PopulateObject } from './filter-schema.factory';
4
+ export { createFilterSchema, FilterSchemaConfig } from './filter-schema.factory';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["utilities/validation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,sDAAoC;AACpC,4DAA0C;AAC1C,iEAAiG;AAAxF,2HAAA,kBAAkB,OAAA","sourcesContent":["export * from './common-validation';\nexport * from './global-validation';\nexport * from './validation-for-frontend';\nexport { createFilterSchema, FilterSchemaConfig, PopulateObject } from './filter-schema.factory';"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["utilities/validation/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,sDAAoC;AACpC,4DAA0C;AAC1C,iEAAiF;AAAxE,2HAAA,kBAAkB,OAAA","sourcesContent":["export * from './common-validation';\nexport * from './global-validation';\nexport * from './validation-for-frontend';\nexport { createFilterSchema, FilterSchemaConfig } from './filter-schema.factory';"]}
@@ -0,0 +1,23 @@
1
+ import dayjs, { Dayjs } from 'dayjs';
2
+ import { HolidayHour, NormalHour, StartEndTime } from '../libraries/mongo/models/shared/work-hour.model';
3
+ import { WeekDay } from './enum';
4
+ export declare const getDaysOfWeek: () => string[];
5
+ export declare const getFullDayHours: () => StartEndTime[];
6
+ export declare const parseTimeString: (timeHHMM: string, zone: string) => dayjs.Dayjs;
7
+ export declare const calculateIntersection: (opening: StartEndTime, valid: StartEndTime, timeZone: string) => StartEndTime | null;
8
+ export declare const getIntersectedHours: (openingHours: StartEndTime[], validHours: StartEndTime[], timeZone?: string) => StartEndTime[];
9
+ export declare const checkWithinNormalHours: (hours: StartEndTime[], normalTimings: NormalHour[], day: keyof typeof WeekDay) => StartEndTime[];
10
+ export declare const checkWithinHolidayHours: (hours: StartEndTime[], holidayTimings: HolidayHour[], currentDate: Dayjs) => StartEndTime[];
11
+ export declare const getHolidayIntersectedHours: (openingHours: StartEndTime[], closingHours: StartEndTime[], timeZone?: string) => StartEndTime[];
12
+ export declare const subtractSegment: (segment: {
13
+ start: dayjs.Dayjs;
14
+ end: dayjs.Dayjs;
15
+ }, block: {
16
+ start: dayjs.Dayjs;
17
+ end: dayjs.Dayjs;
18
+ }) => {
19
+ start: dayjs.Dayjs;
20
+ end: dayjs.Dayjs;
21
+ }[];
22
+ export declare const adminAfterHolidayWorkingHours: (workHourSetting: any) => StartEndTime[];
23
+ export declare const isLyxaOpen: (workHourSetting: any) => boolean;
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isLyxaOpen = exports.adminAfterHolidayWorkingHours = exports.subtractSegment = exports.getHolidayIntersectedHours = exports.checkWithinHolidayHours = exports.checkWithinNormalHours = exports.getIntersectedHours = exports.calculateIntersection = exports.parseTimeString = exports.getFullDayHours = exports.getDaysOfWeek = void 0;
7
+ const dayjs_1 = __importDefault(require("dayjs"));
8
+ const utc_1 = __importDefault(require("dayjs/plugin/utc"));
9
+ const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
10
+ const minMax_1 = __importDefault(require("dayjs/plugin/minMax"));
11
+ const enum_1 = require("./enum");
12
+ const time_1 = require("./time");
13
+ dayjs_1.default.extend(utc_1.default);
14
+ dayjs_1.default.extend(timezone_1.default);
15
+ dayjs_1.default.extend(minMax_1.default);
16
+ const getDaysOfWeek = () => {
17
+ return Array.from({ length: 7 }, (_, i) => (0, dayjs_1.default)().day(i).format('dddd'));
18
+ };
19
+ exports.getDaysOfWeek = getDaysOfWeek;
20
+ const getFullDayHours = () => {
21
+ return [{ start: '0000', end: '2359' }];
22
+ };
23
+ exports.getFullDayHours = getFullDayHours;
24
+ const parseTimeString = (timeHHMM, zone) => {
25
+ const hour = Number(timeHHMM.slice(0, 2));
26
+ const minute = Number(timeHHMM.slice(2));
27
+ return (0, dayjs_1.default)().tz(zone).hour(hour).minute(minute).second(0).millisecond(0);
28
+ };
29
+ exports.parseTimeString = parseTimeString;
30
+ const calculateIntersection = (opening, valid, timeZone) => {
31
+ const openingStart = (0, exports.parseTimeString)(opening.start, timeZone);
32
+ const openingEnd = (0, exports.parseTimeString)(opening.end, timeZone);
33
+ const validStart = (0, exports.parseTimeString)(valid.start, timeZone);
34
+ const validEnd = (0, exports.parseTimeString)(valid.end, timeZone);
35
+ const intersectionStart = dayjs_1.default.max(openingStart, validStart);
36
+ const intersectionEnd = dayjs_1.default.min(openingEnd, validEnd);
37
+ return intersectionStart && intersectionEnd && intersectionStart.isBefore(intersectionEnd)
38
+ ? {
39
+ start: intersectionStart.format('HHmm'),
40
+ end: intersectionEnd.format('HHmm'),
41
+ }
42
+ : null;
43
+ };
44
+ exports.calculateIntersection = calculateIntersection;
45
+ const getIntersectedHours = (openingHours, validHours, timeZone = 'Asia/Dhaka') => {
46
+ const intersections = [];
47
+ for (const opening of openingHours) {
48
+ for (const valid of validHours) {
49
+ const intersection = (0, exports.calculateIntersection)(opening, valid, timeZone);
50
+ if (intersection !== null) {
51
+ intersections.push(intersection);
52
+ }
53
+ }
54
+ }
55
+ return intersections;
56
+ };
57
+ exports.getIntersectedHours = getIntersectedHours;
58
+ const checkWithinNormalHours = (hours, normalTimings, day) => {
59
+ const dayTiming = normalTimings.find(timing => timing.day === enum_1.WeekDay[day]);
60
+ if (!dayTiming)
61
+ return [];
62
+ const statusHandlers = {
63
+ [enum_1.WorkStatus.CLOSED]: () => [],
64
+ [enum_1.WorkStatus.FULL_DAY]: () => (0, exports.getFullDayHours)(),
65
+ [enum_1.WorkStatus.OPEN]: () => dayTiming.openingHours?.length ? (0, exports.getIntersectedHours)(dayTiming.openingHours, hours) : [],
66
+ };
67
+ return statusHandlers[dayTiming.status]?.() || [];
68
+ };
69
+ exports.checkWithinNormalHours = checkWithinNormalHours;
70
+ const checkWithinHolidayHours = (hours, holidayTimings, currentDate) => {
71
+ if (!holidayTimings.length) {
72
+ return hours;
73
+ }
74
+ const currentDateInTimezone = currentDate.tz(time_1.LyxaTimezone);
75
+ const todaysHoliday = holidayTimings.find(holiday => {
76
+ if (!holiday.date)
77
+ return false;
78
+ const holidayDate = (0, dayjs_1.default)(holiday.date).tz(time_1.LyxaTimezone);
79
+ return currentDateInTimezone.isSame(holidayDate, 'day');
80
+ });
81
+ if (!todaysHoliday)
82
+ return hours;
83
+ if (todaysHoliday.status === enum_1.HolidayWorkStatus.FULL_DAY)
84
+ return [];
85
+ if (todaysHoliday.status === enum_1.HolidayWorkStatus.CLOSED &&
86
+ todaysHoliday.closingHour?.start &&
87
+ todaysHoliday.closingHour?.end) {
88
+ return (0, exports.getHolidayIntersectedHours)(hours, [todaysHoliday.closingHour]);
89
+ }
90
+ return hours;
91
+ };
92
+ exports.checkWithinHolidayHours = checkWithinHolidayHours;
93
+ const getHolidayIntersectedHours = (openingHours, closingHours, timeZone = 'Asia/Dhaka') => {
94
+ if (!openingHours.length || !closingHours.length) {
95
+ return openingHours;
96
+ }
97
+ const result = [];
98
+ for (const opening of openingHours) {
99
+ const openingStart = (0, exports.parseTimeString)(opening.start, timeZone);
100
+ const openingEnd = (0, exports.parseTimeString)(opening.end, timeZone);
101
+ let segments = [{ start: openingStart, end: openingEnd }];
102
+ for (const closing of closingHours) {
103
+ const closingStart = (0, exports.parseTimeString)(closing.start, timeZone);
104
+ const closingEnd = (0, exports.parseTimeString)(closing.end, timeZone);
105
+ const block = { start: closingStart, end: closingEnd };
106
+ segments = segments.flatMap(segment => (0, exports.subtractSegment)(segment, block));
107
+ }
108
+ for (const segment of segments) {
109
+ result.push({
110
+ start: segment.start.format('HHmm'),
111
+ end: segment.end.format('HHmm'),
112
+ });
113
+ }
114
+ }
115
+ return result;
116
+ };
117
+ exports.getHolidayIntersectedHours = getHolidayIntersectedHours;
118
+ const subtractSegment = (segment, block) => {
119
+ const { start, end } = segment;
120
+ const { start: blockStart, end: blockEnd } = block;
121
+ if (end.isBefore(blockStart) || start.isAfter(blockEnd))
122
+ return [segment];
123
+ if (blockStart.isBefore(start) && blockEnd.isAfter(end))
124
+ return [];
125
+ if (blockStart.isAfter(start) && blockEnd.isBefore(end)) {
126
+ return [
127
+ { start, end: blockStart },
128
+ { start: blockEnd, end },
129
+ ];
130
+ }
131
+ if (blockStart.isBefore(end) && blockEnd.isBefore(end) && blockEnd.isAfter(start)) {
132
+ return [{ start: blockEnd, end }];
133
+ }
134
+ if (blockEnd.isAfter(start) && blockStart.isAfter(start) && blockStart.isBefore(end)) {
135
+ return [{ start, end: blockStart }];
136
+ }
137
+ return [];
138
+ };
139
+ exports.subtractSegment = subtractSegment;
140
+ const adminAfterHolidayWorkingHours = (workHourSetting) => {
141
+ const fullDayHours = (0, exports.getFullDayHours)();
142
+ const now = (0, dayjs_1.default)().tz(time_1.LyxaTimezone);
143
+ const currentDayIndex = now.day();
144
+ const currentDay = (0, exports.getDaysOfWeek)()[currentDayIndex];
145
+ const currentDate = (0, dayjs_1.default)().utc().startOf('day');
146
+ const adminNormalWorkingHours = (0, exports.checkWithinNormalHours)(fullDayHours, workHourSetting.normalHours, currentDay);
147
+ return (0, exports.checkWithinHolidayHours)(adminNormalWorkingHours, workHourSetting.holidayHours, currentDate);
148
+ };
149
+ exports.adminAfterHolidayWorkingHours = adminAfterHolidayWorkingHours;
150
+ const isLyxaOpen = (workHourSetting) => {
151
+ const currentTime = new Date().getHours() * 100 + new Date().getMinutes();
152
+ const lyxaOpenHour = (0, exports.adminAfterHolidayWorkingHours)(workHourSetting);
153
+ return lyxaOpenHour.some(timing => parseInt(timing.start) <= currentTime && currentTime <= parseInt(timing.end));
154
+ };
155
+ exports.isLyxaOpen = isLyxaOpen;
156
+ //# sourceMappingURL=workHourSetting.js.map
@@ -0,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uBAAuB,GAAG,IAAA,8BAAsB,EACrD,YAAY,EACZ,eAAe,CAAC,WAAW,EAC3B,UAAU,CACV,CAAC;IAEF,OAAO,IAAA,+BAAuB,EAAC,uBAAuB,EAAE,eAAe,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;AACpG,CAAC,CAAC;AAfW,QAAA,6BAA6B,iCAexC;AAEK,MAAM,UAAU,GAAG,CAAC,eAAoB,EAAE,EAAE;IAClD,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC,QAAQ,EAAE,GAAG,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,UAAU,EAAE,CAAC;IAC1E,MAAM,YAAY,GAAG,IAAA,qCAA6B,EAAC,eAAe,CAAC,CAAC;IACpE,OAAO,YAAY,CAAC,IAAI,CACvB,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,WAAW,IAAI,WAAW,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CACtF,CAAC;AACH,CAAC,CAAC;AANW,QAAA,UAAU,cAMrB","sourcesContent":["import dayjs, { Dayjs } from 'dayjs';\nimport utc from 'dayjs/plugin/utc';\nimport timezone from 'dayjs/plugin/timezone';\nimport minMax from 'dayjs/plugin/minMax';\nimport { HolidayHour, NormalHour, StartEndTime } from '../libraries/mongo/models/shared/work-hour.model';\nimport { HolidayWorkStatus, WeekDay, WorkStatus } from './enum';\nimport { LyxaTimezone } from './time';\n\ndayjs.extend(utc);\ndayjs.extend(timezone);\ndayjs.extend(minMax);\n\nexport const getDaysOfWeek = (): string[] => {\n\treturn Array.from({ length: 7 }, (_, i) => dayjs().day(i).format('dddd'));\n};\n\nexport const getFullDayHours = (): StartEndTime[] => {\n\treturn [{ start: '0000', end: '2359' }];\n};\n\nexport const parseTimeString = (timeHHMM: string, zone: string): dayjs.Dayjs => {\n\tconst hour = Number(timeHHMM.slice(0, 2));\n\tconst minute = Number(timeHHMM.slice(2));\n\treturn dayjs().tz(zone).hour(hour).minute(minute).second(0).millisecond(0);\n};\nexport const calculateIntersection = (\n\topening: StartEndTime,\n\tvalid: StartEndTime,\n\ttimeZone: string\n): StartEndTime | null => {\n\tconst openingStart = parseTimeString(opening.start, timeZone);\n\tconst openingEnd = parseTimeString(opening.end, timeZone);\n\tconst validStart = parseTimeString(valid.start, timeZone);\n\tconst validEnd = parseTimeString(valid.end, timeZone);\n\n\tconst intersectionStart = dayjs.max(openingStart, validStart);\n\tconst intersectionEnd = dayjs.min(openingEnd, validEnd);\n\n\treturn intersectionStart && intersectionEnd && intersectionStart.isBefore(intersectionEnd)\n\t\t? {\n\t\t\t\tstart: intersectionStart.format('HHmm'),\n\t\t\t\tend: intersectionEnd.format('HHmm'),\n\t\t\t}\n\t\t: null;\n};\n\nexport const getIntersectedHours = (\n\topeningHours: StartEndTime[],\n\tvalidHours: StartEndTime[],\n\ttimeZone = 'Asia/Dhaka'\n): StartEndTime[] => {\n\tconst intersections: StartEndTime[] = [];\n\n\tfor (const opening of openingHours) {\n\t\tfor (const valid of validHours) {\n\t\t\tconst intersection = calculateIntersection(opening, valid, timeZone);\n\n\t\t\tif (intersection !== null) {\n\t\t\t\tintersections.push(intersection);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn intersections;\n};\n\nexport const checkWithinNormalHours = (\n\thours: StartEndTime[],\n\tnormalTimings: NormalHour[],\n\tday: keyof typeof WeekDay\n): StartEndTime[] => {\n\tconst dayTiming = normalTimings.find(timing => timing.day === WeekDay[day]);\n\n\tif (!dayTiming) return [];\n\n\tconst statusHandlers = {\n\t\t[WorkStatus.CLOSED]: () => [],\n\t\t[WorkStatus.FULL_DAY]: () => getFullDayHours(),\n\t\t[WorkStatus.OPEN]: () =>\n\t\t\tdayTiming.openingHours?.length ? getIntersectedHours(dayTiming.openingHours, hours) : [],\n\t};\n\n\treturn statusHandlers[dayTiming.status]?.() || [];\n};\n\nexport const checkWithinHolidayHours = (\n\thours: StartEndTime[],\n\tholidayTimings: HolidayHour[],\n\tcurrentDate: Dayjs\n): StartEndTime[] => {\n\tif (!holidayTimings.length) {\n\t\treturn hours;\n\t}\n\n\t/** Find If Today Is Holiday */\n\tconst currentDateInTimezone = currentDate.tz(LyxaTimezone);\n\tconst todaysHoliday = holidayTimings.find(holiday => {\n\t\tif (!holiday.date) return false;\n\t\tconst holidayDate = dayjs(holiday.date).tz(LyxaTimezone);\n\t\treturn currentDateInTimezone.isSame(holidayDate, 'day');\n\t});\n\n\t/** No holiday today - return original hours */\n\tif (!todaysHoliday) return hours;\n\n\t/** Handle holiday status --> Full Day Holiday */\n\tif (todaysHoliday.status === HolidayWorkStatus.FULL_DAY) return [];\n\n\t/** Handle holiday status --> Certain Hour Closed Holiday */\n\tif (\n\t\ttodaysHoliday.status === HolidayWorkStatus.CLOSED &&\n\t\ttodaysHoliday.closingHour?.start &&\n\t\ttodaysHoliday.closingHour?.end\n\t) {\n\t\treturn getHolidayIntersectedHours(hours, [todaysHoliday.closingHour]);\n\t}\n\n\t/** Default fallback - return original hours */\n\treturn hours;\n};\n\nexport const getHolidayIntersectedHours = (\n\topeningHours: StartEndTime[],\n\tclosingHours: StartEndTime[],\n\ttimeZone = 'Asia/Dhaka'\n): StartEndTime[] => {\n\tif (!openingHours.length || !closingHours.length) {\n\t\treturn openingHours;\n\t}\n\n\tconst result: StartEndTime[] = [];\n\n\tfor (const opening of openingHours) {\n\t\tconst openingStart = parseTimeString(opening.start, timeZone);\n\t\tconst openingEnd = parseTimeString(opening.end, timeZone);\n\n\t\tlet segments = [{ start: openingStart, end: openingEnd }];\n\n\t\t/** Applying each closing period to current segments */\n\t\tfor (const closing of closingHours) {\n\t\t\tconst closingStart = parseTimeString(closing.start, timeZone);\n\t\t\tconst closingEnd = parseTimeString(closing.end, timeZone);\n\t\t\tconst block = { start: closingStart, end: closingEnd };\n\n\t\t\tsegments = segments.flatMap(segment => subtractSegment(segment, block));\n\t\t}\n\n\t\t/** Converting segments to result format */\n\t\tfor (const segment of segments) {\n\t\t\tresult.push({\n\t\t\t\tstart: segment.start.format('HHmm'),\n\t\t\t\tend: segment.end.format('HHmm'),\n\t\t\t});\n\t\t}\n\t}\n\n\treturn result;\n};\nexport const subtractSegment = (\n\tsegment: { start: dayjs.Dayjs; end: dayjs.Dayjs },\n\tblock: { start: dayjs.Dayjs; end: dayjs.Dayjs }\n): { start: dayjs.Dayjs; end: dayjs.Dayjs }[] => {\n\tconst { start, end } = segment;\n\tconst { start: blockStart, end: blockEnd } = block;\n\n\t/** Sequence Matter */\n\t/** No Overlap */\n\tif (end.isBefore(blockStart) || start.isAfter(blockEnd)) return [segment];\n\n\t/** Fully Overlapped */\n\tif (blockStart.isBefore(start) && blockEnd.isAfter(end)) return [];\n\n\t/** Block Inside Segment (Middle Cut) */\n\tif (blockStart.isAfter(start) && blockEnd.isBefore(end)) {\n\t\treturn [\n\t\t\t{ start, end: blockStart },\n\t\t\t{ start: blockEnd, end },\n\t\t];\n\t}\n\n\t/** Overlap At Start */\n\tif (blockStart.isBefore(end) && blockEnd.isBefore(end) && blockEnd.isAfter(start)) {\n\t\treturn [{ start: blockEnd, end }];\n\t}\n\n\t/** Overlap At End */\n\tif (blockEnd.isAfter(start) && blockStart.isAfter(start) && blockStart.isBefore(end)) {\n\t\treturn [{ start, end: blockStart }];\n\t}\n\n\treturn [];\n};\n\nexport const adminAfterHolidayWorkingHours = (workHourSetting: any) => {\n\tconst fullDayHours = getFullDayHours();\n\tconst now = dayjs().tz(LyxaTimezone);\n\tconst currentDayIndex = now.day(); // 0 (Sunday) through 6 (Saturday)\n\tconst currentDay = getDaysOfWeek()[currentDayIndex] as keyof typeof WeekDay;\n\tconst currentDate = dayjs().utc().startOf('day');\n\n\t/**Admin Normal Working Hour*/\n\tconst adminNormalWorkingHours = checkWithinNormalHours(\n\t\tfullDayHours,\n\t\tworkHourSetting.normalHours,\n\t\tcurrentDay\n\t);\n\t/**Admin Working Hour After Subtracting From Admin Holiday Hour */\n\treturn checkWithinHolidayHours(adminNormalWorkingHours, workHourSetting.holidayHours, currentDate);\n};\n\nexport const isLyxaOpen = (workHourSetting: any) => {\n\tconst currentTime = new Date().getHours() * 100 + new Date().getMinutes();\n\tconst lyxaOpenHour = adminAfterHolidayWorkingHours(workHourSetting);\n\treturn lyxaOpenHour.some(\n\t\ttiming => parseInt(timing.start) <= currentTime && currentTime <= parseInt(timing.end)\n\t);\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyxa.ai/core",
3
- "version": "1.1.59",
3
+ "version": "1.1.61",
4
4
  "description": "The Core system of the Lyxa services.",
5
5
  "author": "elie <42282499+Internalizable@users.noreply.github.com>",
6
6
  "license": "MIT",