@webitel/api-services 26.8.62 → 26.8.64

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/api-services",
3
- "version": "26.8.62",
3
+ "version": "26.8.64",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,7 +11,8 @@ import type { FormatDateMode } from '../enums/FormatDateMode/FormatDateMode';
11
11
  */
12
12
  const TIMEZONE_STORAGE_KEY = 'user-timezone-setting';
13
13
 
14
- function getUserTimeZone(): string {
14
+ /** The operator's chosen time zone, falling back to the browser's. */
15
+ export function getUserTimeZone(): string {
15
16
  return (
16
17
  localStorage.getItem(TIMEZONE_STORAGE_KEY) ||
17
18
  Intl.DateTimeFormat().resolvedOptions().timeZone
@@ -0,0 +1,73 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { z } from 'zod';
3
+
4
+ import { filledLookupSchema } from '../../_shared/lookup.validations';
5
+ import { caseCloseFieldsChecks, caseSchema } from '../case.validations';
6
+
7
+ const schema = caseSchema
8
+ .extend({
9
+ source: filledLookupSchema,
10
+ service: filledLookupSchema,
11
+ })
12
+ .check(...caseCloseFieldsChecks);
13
+
14
+ const issuePaths = (value: unknown) =>
15
+ schema.safeParse(value).error?.issues.map(({ path }) => path.join('.')) ?? [];
16
+
17
+ describe('caseCloseFieldsChecks', () => {
18
+ const finalStatusCondition = {
19
+ id: '6',
20
+ name: 'Closed',
21
+ final: true,
22
+ };
23
+
24
+ it('requires close fields on a final case while other fields are still unset', () => {
25
+ const paths = issuePaths({
26
+ statusCondition: finalStatusCondition,
27
+ closeReason: null,
28
+ closeResult: '',
29
+ });
30
+
31
+ expect(paths).toEqual(
32
+ expect.arrayContaining([
33
+ 'source',
34
+ 'service',
35
+ 'closeReason',
36
+ 'closeResult',
37
+ ]),
38
+ );
39
+ });
40
+
41
+ it('points an empty close reason object at its id', () => {
42
+ expect(
43
+ issuePaths({
44
+ statusCondition: finalStatusCondition,
45
+ closeReason: {},
46
+ closeResult: 'a result',
47
+ }),
48
+ ).toContain('closeReason.id');
49
+ });
50
+
51
+ it('does not require close fields on a non-final case', () => {
52
+ const paths = issuePaths({
53
+ statusCondition: {
54
+ id: '5',
55
+ name: 'New',
56
+ initial: true,
57
+ },
58
+ });
59
+
60
+ expect(paths).not.toContain('closeReason');
61
+ expect(paths).not.toContain('closeResult');
62
+ });
63
+
64
+ it('does not throw when the parsed value is not an object', () => {
65
+ expect(() => schema.safeParse(undefined)).not.toThrow();
66
+ expect(() =>
67
+ z
68
+ .object({})
69
+ .check(...caseCloseFieldsChecks)
70
+ .safeParse(null),
71
+ ).not.toThrow();
72
+ });
73
+ });
@@ -33,35 +33,42 @@ export const caseSchema = z.object({
33
33
  closeResult: z.string().default(''),
34
34
  } satisfies Partial<Record<keyof WebitelCasesCase, z.ZodType>>);
35
35
 
36
- export const refineCaseCloseFields = (
37
- data: Pick<
38
- z.infer<typeof caseSchema>,
39
- 'statusCondition' | 'closeReason' | 'closeResult'
40
- >,
41
- ctx: z.RefinementCtx,
42
- ) => {
43
- if (!data.statusCondition?.final) return;
36
+ type CaseCloseFields = Pick<
37
+ z.infer<typeof caseSchema>,
38
+ 'statusCondition' | 'closeReason' | 'closeResult'
39
+ >;
44
40
 
45
- if (!data.closeReason?.id)
46
- ctx.addIssue({
47
- code: 'custom',
48
- path: data.closeReason
49
- ? [
50
- 'closeReason',
51
- 'id',
52
- ]
53
- : [
54
- 'closeReason',
55
- ],
41
+ const requiredOnFinalCase = (
42
+ isFilled: (data: CaseCloseFields) => boolean,
43
+ path: string[],
44
+ ) =>
45
+ z.refine<CaseCloseFields>(
46
+ (data) => !data?.statusCondition?.final || isFilled(data),
47
+ {
48
+ path,
49
+ when: () => true,
56
50
  ...i18nIssue('required'),
57
- });
51
+ },
52
+ );
58
53
 
59
- if (!data.closeResult)
60
- ctx.addIssue({
61
- code: 'custom',
62
- path: [
63
- 'closeResult',
64
- ],
65
- ...i18nIssue('required'),
66
- });
67
- };
54
+ export const caseCloseFieldsChecks = [
55
+ requiredOnFinalCase(
56
+ (data) => Boolean(data.closeReason),
57
+ [
58
+ 'closeReason',
59
+ ],
60
+ ),
61
+ requiredOnFinalCase(
62
+ (data) => !data.closeReason || Boolean(data.closeReason.id),
63
+ [
64
+ 'closeReason',
65
+ 'id',
66
+ ],
67
+ ),
68
+ requiredOnFinalCase(
69
+ (data) => Boolean(data.closeResult),
70
+ [
71
+ 'closeResult',
72
+ ],
73
+ ),
74
+ ];