summaryrefslogtreecommitdiff
path: root/src-migrate/validations/auth.ts
blob: 3abdfb573648af4250b52ff76d7489b58816e5bf (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { z } from 'zod';

export const registerSchema = z
  .object({
    name: z.string().min(1, { message: 'Nama harus diisi' }),
    email: z
      .string()
      .min(1, { message: 'Email harus diisi' })
      .email({ message: 'Email harus menggunakan format example@mail.com' }),
    password: z.string().min(6, { message: 'Password minimal 6 karakter' }),
    phone: z
      .string()
      .min(1, { message: 'Nomor telepon harus diisi' })
      .refine((val) => /^\d{10,12}$/.test(val), {
        message: 'Format nomor telepon tidak valid, contoh: 081234567890',
      }),
    type_acc: z.string().optional(),
    nama_wajib_pajak: z.string().optional(),
    alamat_bisnis: z.string().optional(),
    alamat_wajib_pajak: z.string().optional(),
    is_pkp: z.string(),
    is_terdaftar: z.string(),
    sppkp_document: z.string().optional(),
    npwp_document: z.string().optional(),
    industry_id: z.string().optional(),
    email_partner: z.string().optional(),
    business_name: z.string().optional(),
    company_type_id: z.string().optional(),
    isChekBox: z.string().optional(),
    npwp: z.string().optional().refine((val) => !val || /^\d{15,16}$/.test(val), {
      message: 'Format NPWP tidak valid, NPWP harus terdiri dari 15-16 digit angka.',
    }),
    sppkp: z.string().optional(),
  })
  .superRefine((data, ctx) => {
    if (data.type_acc === 'business') {
      if (data.is_terdaftar === 'false') {
        if (data.is_pkp === 'true') {
          const requiredFields: { field: keyof typeof data; message: string }[] = [
            { field: 'business_name', message: 'Nama perusahaan harus diisi' },
            { field: 'alamat_bisnis', message: 'Alamat perusahaan harus diisi' },
            // { field: 'alamat_wajib_pajak', message: 'Alamat wajib pajak harus diisi' },
            { field: 'company_type_id', message: 'Badan usaha wajib dipilih' },
            { field: 'industry_id', message: 'Jenis usaha harus dipilih' },
            { field: 'sppkp_document', message: 'Document harus diisi' },
            { field: 'npwp_document', message: 'Document harus diisi' },
            { field: 'npwp', message: 'Format NPWP tidak valid, NPWP harus terdiri dari 15-16 digit angka.' },
            { field: 'nama_wajib_pajak', message: 'Nama wajib pajak harus diisi' },
          ];

          requiredFields.forEach(({ field, message }) => {
            if (!data[field]) {
              ctx.addIssue({
                code: 'custom',
                path: [field],
                message,
              });
            }
          });
          
          if (!data.email_partner || !z.string().email().safeParse(data.email_partner).success) {
            ctx.addIssue({
              code: 'custom',
              path: ['email_partner'],
              message: 'Email partner harus diisi dengan format example@mail.com',
            });
          }
          if(data.isChekBox === 'false'){
            if (!data.alamat_wajib_pajak) {
              ctx.addIssue({
                code: 'custom',
                path: ['alamat_wajib_pajak'],
                message: 'Alamat wajib pajak harus diisi',
              });
            }
          }

        } else {
          if (!data.business_name) {
            ctx.addIssue({
              code: 'custom',
              path: ['business_name'],
              message: 'Nama perusahaan harus diisi',
            });
          }
          if (!data.alamat_bisnis) {
            ctx.addIssue({
              code: 'custom',
              path: ['alamat_bisnis'],
              message: 'Alamat perusahaan harus diisi',
            });
          }

          if (!data.company_type_id) {
            ctx.addIssue({
              code: 'custom',
              path: ['company_type_id'],
              message: 'Badan usaha wajib dipilih',
            });
          }
          if (!data.industry_id) {
            ctx.addIssue({
              code: 'custom',
              path: ['industry_id'],
              message: 'Jenis usaha harus dipilih',
            });
          }

          if (data.npwp && !/^\d{15,16}$/.test(data.npwp)) {
            ctx.addIssue({
              code: 'custom',
              path: ['npwp'],
              message: 'Format NPWP tidak valid, NPWP harus terdiri dari 15-16 digit angka.',
            });
          }
          
        }
      }else{
        if (data.is_pkp === 'true') {
          if (!data.business_name) {
            ctx.addIssue({
              code: 'custom',
              path: ['business_name'],
              message: 'Nama perusahaan harus diisi',
            });
          }
        } else {
          if (!data.business_name) {
            ctx.addIssue({
              code: 'custom',
              path: ['business_name'],
              message: 'Nama perusahaan harus diisi',
            });
          }
        }
      }

      // Remove this unconditional issue addition to prevent blocking form submission
      // ctx.addIssue({
      //   code: 'custom',
      //   path: ['business_name'],
      //   message: 'Nama perusahaan harus diisi',
      // });
    }else{

    }
  });