storemw-core-api 1.0.68 → 1.0.69
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/dist/schema/payload/account/schemaAccount.d.ts +20 -20
- package/dist/services/auth/AuthService.js +7 -6
- package/dist/services/auth/AuthService.js.map +1 -1
- package/dist/services/user/UserService.d.ts +2 -1
- package/dist/services/user/UserService.js +6 -2
- package/dist/services/user/UserService.js.map +1 -1
- package/package.json +1 -1
|
@@ -116,11 +116,6 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
116
116
|
password: string;
|
|
117
117
|
address?: string | undefined;
|
|
118
118
|
};
|
|
119
|
-
account: {
|
|
120
|
-
status: boolean;
|
|
121
|
-
name: string;
|
|
122
|
-
accountCode: string;
|
|
123
|
-
};
|
|
124
119
|
business: {
|
|
125
120
|
email: string;
|
|
126
121
|
postcode: string;
|
|
@@ -134,6 +129,11 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
134
129
|
areaId: number;
|
|
135
130
|
countryId: number;
|
|
136
131
|
};
|
|
132
|
+
account: {
|
|
133
|
+
status: boolean;
|
|
134
|
+
name: string;
|
|
135
|
+
accountCode: string;
|
|
136
|
+
};
|
|
137
137
|
}, {
|
|
138
138
|
administrator: {
|
|
139
139
|
firstname: string;
|
|
@@ -144,11 +144,6 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
144
144
|
password: string;
|
|
145
145
|
address?: string | undefined;
|
|
146
146
|
};
|
|
147
|
-
account: {
|
|
148
|
-
status: boolean;
|
|
149
|
-
name: string;
|
|
150
|
-
accountCode: string;
|
|
151
|
-
};
|
|
152
147
|
business: {
|
|
153
148
|
email: string;
|
|
154
149
|
postcode: string;
|
|
@@ -162,6 +157,11 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
162
157
|
areaId: number;
|
|
163
158
|
countryId: number;
|
|
164
159
|
};
|
|
160
|
+
account: {
|
|
161
|
+
status: boolean;
|
|
162
|
+
name: string;
|
|
163
|
+
accountCode: string;
|
|
164
|
+
};
|
|
165
165
|
}>;
|
|
166
166
|
}, "strip", z.ZodTypeAny, {
|
|
167
167
|
scope: {
|
|
@@ -178,11 +178,6 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
178
178
|
password: string;
|
|
179
179
|
address?: string | undefined;
|
|
180
180
|
};
|
|
181
|
-
account: {
|
|
182
|
-
status: boolean;
|
|
183
|
-
name: string;
|
|
184
|
-
accountCode: string;
|
|
185
|
-
};
|
|
186
181
|
business: {
|
|
187
182
|
email: string;
|
|
188
183
|
postcode: string;
|
|
@@ -196,6 +191,11 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
196
191
|
areaId: number;
|
|
197
192
|
countryId: number;
|
|
198
193
|
};
|
|
194
|
+
account: {
|
|
195
|
+
status: boolean;
|
|
196
|
+
name: string;
|
|
197
|
+
accountCode: string;
|
|
198
|
+
};
|
|
199
199
|
};
|
|
200
200
|
}, {
|
|
201
201
|
scope: {
|
|
@@ -212,11 +212,6 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
212
212
|
password: string;
|
|
213
213
|
address?: string | undefined;
|
|
214
214
|
};
|
|
215
|
-
account: {
|
|
216
|
-
status: boolean;
|
|
217
|
-
name: string;
|
|
218
|
-
accountCode: string;
|
|
219
|
-
};
|
|
220
215
|
business: {
|
|
221
216
|
email: string;
|
|
222
217
|
postcode: string;
|
|
@@ -230,6 +225,11 @@ export declare const schemaAccountCreatePayload: z.ZodObject<{
|
|
|
230
225
|
areaId: number;
|
|
231
226
|
countryId: number;
|
|
232
227
|
};
|
|
228
|
+
account: {
|
|
229
|
+
status: boolean;
|
|
230
|
+
name: string;
|
|
231
|
+
accountCode: string;
|
|
232
|
+
};
|
|
233
233
|
};
|
|
234
234
|
}>;
|
|
235
235
|
export type SchemaAccountCreatePayload = z.infer<typeof schemaAccountCreatePayload>;
|
|
@@ -150,7 +150,7 @@ const AuthService = (props) => {
|
|
|
150
150
|
const accountId = jwtData?.account?.account_id ?? "";
|
|
151
151
|
const userId = jwtData?.user?.user_id ?? "";
|
|
152
152
|
const userType = jwtData?.user?.user_type ?? "";
|
|
153
|
-
if (!accountId) {
|
|
153
|
+
if (!accountId && userType !== "operator") { // operator token do not have account id
|
|
154
154
|
(0, utils_1.throwError)(`Invalid or missing account id in auth token`);
|
|
155
155
|
}
|
|
156
156
|
if (!userId) {
|
|
@@ -163,15 +163,15 @@ const AuthService = (props) => {
|
|
|
163
163
|
const accountService = (0, services_1.AccountService)({ ...rest, isOperator: true });
|
|
164
164
|
const user = await userService.getUser({ id: Number(userId), datatypes: [], includePassword: true });
|
|
165
165
|
const account = await accountService.getAccount({ id: Number(accountId), datatypes: [] });
|
|
166
|
-
console.log('jwtData', jwtData)
|
|
167
|
-
console.log('user', user)
|
|
168
|
-
console.log('account', account)
|
|
166
|
+
// console.log('jwtData', jwtData)
|
|
167
|
+
// console.log('user', user)
|
|
168
|
+
// console.log('account', account)
|
|
169
169
|
// const a = await hashPassword(user?.password)
|
|
170
170
|
// check the accountId / userId is valid
|
|
171
171
|
if (!user?.user_id) {
|
|
172
172
|
(0, utils_1.throwError)(`Invalid user id in auth token`);
|
|
173
173
|
}
|
|
174
|
-
if (!account?.account_id) {
|
|
174
|
+
if (!account?.account_id && userType !== "operator") { // operator token do not have account id
|
|
175
175
|
(0, utils_1.throwError)(`Invalid account id in auth token`);
|
|
176
176
|
}
|
|
177
177
|
// new password cannot same with current password
|
|
@@ -198,7 +198,8 @@ const AuthService = (props) => {
|
|
|
198
198
|
id: Number(userId),
|
|
199
199
|
data: {
|
|
200
200
|
loginPassword: await (0, utils_2.hashPassword)(newPassword)
|
|
201
|
-
}
|
|
201
|
+
},
|
|
202
|
+
isOperator: Boolean(userType === "operator")
|
|
202
203
|
});
|
|
203
204
|
return responseUser;
|
|
204
205
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthService.js","sourceR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;YAE9C,IAAI,CAAC,aAAa,EAAE,CAAC;gBACjB,MAAM,KAAK,CAAC,0BAA0B,CAAC,CAAA;YAC3C,CAAC;QAEL,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YAClB,IAAA,kBAAU,EAAC,eAAe,KAAK,EAAE,OAAO,EAAE,CAAC,CAAA;QAC/C,CAAC;QAED,4BAA4B;QAC5B,MAAM,YAAY,GAAG,MAAM,WAAW,CAAC,eAAe,CAAC;YACnD,EAAE,EAAE,MAAM,CAAC,MAAM,CAAC;YAClB,IAAI,EAAE;gBACF,aAAa,EAAE,MAAM,IAAA,oBAAY,EAAC,WAAW,CAAC;aACjD;SACJ,CAAC,CAAA;QAEF,OAAO,YAAY,CAAA;IAEvB,CAAC,CAAA;IAED,OAAO;QACH,YAAY;QACZ,eAAe,EAAf,uBAAe;QACf,cAAc;KACjB,CAAA;AAEL,CAAC,CAAA;AA3PY,QAAA,WAAW,eA2PvB","sourcesContent":["// import { QueryList, QueryGet } from \"@/schema/common\";\nimport { DefaultServiceProps, throwError } from \"@/utils\";\nimport {\n UserService,\n UserGetProps,\n UserType,\n UserListProps,\n userTypes,\n AccountService,\n LocationService,\n LocationListProps\n} from \"@/services\";\n\nimport jwt, { JwtPayload } from \"jsonwebtoken\";\nimport { config } from \"@/configs/config\";\n\nimport { generateAuthToken, verifyPassword, verifyAuthToken, hashPassword } from \"@/utils\"\nimport { ModelAccountFields } from \"@/models\";\n\nexport const authTypes = {\n operator: \"operator\",\n administrator: \"administrator\",\n worker: \"worker\",\n customer: \"customer\",\n} as const\n\nexport type AuthType = typeof authTypes[keyof typeof authTypes]\n\nexport type AuthJwtToken = JwtPayload & {\n user?: { id: number, user_id: number, user_type: AuthType };\n account?: { id: number, account_id: number };\n};\n\nexport type AuthServiceProps = DefaultServiceProps & {\n secretKey: string,\n authType: AuthType\n};\n\nexport type AuthGetTokenProps = {\n authType: AuthType,\n accountId?: number,\n accountCode?: string,\n data:\n // get token\n {\n username: string,\n password: string\n }\n};\n\nexport type AuthChangePasswordProps = {\n authToken: string,\n currentPassword: string\n newPassword: string\n confirmPassword: string\n};\n\nexport const AuthService = (props: AuthServiceProps) => {\n\n const {\n secretKey,\n authType = \"administrator\",\n // prisma,\n // authType = \"administrator\",\n // accountId = 0,\n // actionUserId = 0,\n ...rest\n } = props\n\n const getUserInjectionFieldName = (authType: AuthType) => {\n\n if (authType === \"administrator\") return [\"administrator_injection_fields\"]\n if (authType === \"customer\") return [\"customer_injection_fields\"]\n if (authType === \"worker\") return [\"worker_injection_fields\"]\n\n return []\n }\n\n const getAuthToken = async ({ authType, data, accountId = 0, accountCode = \"\" }: AuthGetTokenProps) => {\n\n let _rest = rest\n\n // check the option pass in using account id or account code\n if (authType !== \"operator\") {\n // _rest.accountId = accountId\n\n // check if the accountId & accountCode is not empty\n if (!accountId && !accountCode) throwError(`accountId or accountCode is required`)\n\n let selectAccountId = `${accountId}`\n\n // check account id is choose \n if (selectAccountId === \"0\") {\n\n const globalAccountService = AccountService({ ...rest, isOperator: true })\n\n // use account code to find the account id\n if (accountCode) {\n\n let { data: responseAcc } = await globalAccountService.listAccounts({\n limit: 1,\n offset: 0,\n sortfield: `${ModelAccountFields.account_id}`,\n sortorder: `ASC`,\n filters: [{\n field: `${ModelAccountFields.account_code}`,\n operator: `=`,\n value: `${accountCode}`,\n }],\n datatypes: []\n })\n\n selectAccountId = responseAcc.length > 0 ? responseAcc[0].account_id : \"\"\n let selectAccountStatus = responseAcc.length > 0 ? responseAcc[0].account_status : \"\"\n\n if (selectAccountStatus !== true) throwError(`Inactive account: ${accountCode}`)\n\n }\n\n if (!selectAccountId) {\n throwError(`Invalid account code: ${accountCode}`)\n }\n\n }\n\n _rest.accountId = Number(selectAccountId)\n\n } else {\n _rest.accountId = accountId\n }\n\n const userService = UserService({ userType: authType as UserType, ...rest })\n const accountService = AccountService({ ...rest, isOperator: true })\n const locationService = LocationService({ ...rest, locationType: \"location\" })\n\n // let data: any = {}\n\n let payload: UserListProps = {\n includePassword: true,\n limit: 1,\n offset: 0,\n sortfield: \"user_id\",\n sortorder: \"ASC\",\n filters: [\n { field: \"username\", operator: \"=\", value: `${data[\"username\"]}` },\n { field: \"user_type\", operator: \"=\", value: `${authType}` }\n ],\n datatypes: getUserInjectionFieldName(authType)\n }\n\n const { data: foundUser } = await userService.listUsers(payload)\n\n if (foundUser.length <= 0) {\n throw Error(`Invalid username: ${data[\"username\"]}`)\n }\n\n let user = foundUser.length > 0 ? foundUser[0] : {}\n\n let decryptedPass: boolean\n\n try {\n decryptedPass = await verifyPassword(data.password, user.password)\n } catch (error) {\n throw error\n }\n\n if (decryptedPass === false) {\n throw Error(`Invalid credential: ${data[\"username\"]}`)\n }\n\n // dont show out to result\n delete user.password\n\n let account: any = {}\n\n if (_rest.accountId && authType !== \"operator\") {\n account = await accountService.getAccount({ id: Number(_rest.accountId), datatypes: [] })\n }\n\n const authToken = generateAuthToken(user, account, secretKey)\n\n // get the locations for user\n const locationIds = user?.location_id ? user.location_id.split(\", \") : []\n\n let locations: any[] = []\n\n if (locationIds.length > 0) {\n const listPayload: LocationListProps = {\n limit: 100,\n offset: 0,\n sortfield: \"location_id\",\n sortorder: \"ASC\",\n filters: [{\n field: \"location_id\",\n operator: \"where_in\",\n value: locationIds.map((i: any) => Number(i))\n }],\n datatypes: []\n }\n\n const { data } = await locationService.listLocations(listPayload)\n locations = data\n }\n\n return {\n token: authToken,\n account,\n user,\n user_locations: locations,\n parent_user_id: 0,\n roles: [\n // {\n // id: 23,\n // role_name: \"123\"\n // }\n ],\n permissions: [],\n is_owner: Boolean(user?.is_owner === 1),\n owner_id: 0\n }\n };\n\n const changePassword = async ({ authToken, currentPassword, confirmPassword, newPassword }: AuthChangePasswordProps) => {\n\n const authSecretKey = config.AUTH_SECRET_KEY\n\n // validate the token\n const jwtData = verifyAuthToken(authToken, authSecretKey);\n\n const accountId = jwtData?.account?.account_id ?? \"\";\n const userId = jwtData?.user?.user_id ?? \"\";\n const userType = jwtData?.user?.user_type ?? \"\";\n\n if (!accountId) {\n throwError(`Invalid or missing account id in auth token`)\n }\n\n if (!userId) {\n throwError(`Missing user id in auth token`)\n }\n\n if (!userType) {\n throwError(`Missing user type in auth token`)\n }\n\n const userService = UserService({ userType, ...rest })\n const accountService = AccountService({ ...rest, isOperator: true })\n\n const user = await userService.getUser({ id: Number(userId), datatypes: [], includePassword: true })\n const account = await accountService.getAccount({ id: Number(accountId), datatypes: [] })\n\n\n console.log('jwtData', jwtData)\n console.log('user', user)\n console.log('account', account)\n\n // const a = await hashPassword(user?.password)\n\n // check the accountId / userId is valid\n if (!user?.user_id) {\n throwError(`Invalid user id in auth token`)\n }\n\n if (!account?.account_id) {\n throwError(`Invalid account id in auth token`)\n }\n\n // new password cannot same with current password\n if (newPassword === currentPassword) {\n throwError(`New and current password cannot be same`)\n }\n\n // new password must same with confirm\n if (newPassword !== confirmPassword) {\n throwError(`New and confirm password not matched`)\n }\n\n // check the current password\n try {\n const decryptedPass = await verifyPassword(currentPassword, user?.password)\n // console.log('decryptedPass', decryptedPass)\n\n if (!decryptedPass) {\n throw Error(`Invalid current password`)\n }\n\n } catch (error: any) {\n throwError(`Auth Token: ${error?.message}`)\n }\n\n // perform update credential\n const responseUser = await userService.replacePassword({\n id: Number(userId),\n data: {\n loginPassword: await hashPassword(newPassword)\n }\n })\n\n return responseUser\n\n }\n\n return {\n getAuthToken,\n verifyAuthToken,\n changePassword\n }\n\n}\n\n"]}
|
|
1
|
+
{"version":3,"file":"AuthService.js","sourceRoot":"","sources":["../../../src/services/auth/AuthService.ts"],"names":[],"mappings":";;;AAAA,yDAAyD;AACzD,mCAA0D;AAC1D,yCASoB;AAGpB,6CAA0C;AAE1C,mCAA0F;AAC1F,qCAA8C;AAEjC,QAAA,SAAS,GAAG;IACrB,QAAQ,EAAE,UAAU;IACpB,aAAa,EAAE,eAAe;IAC9B,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;CACd,CAAA;AAiCH,MAAM,WAAW,GAAG,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a,EAAE,MAAM,IAAA,oBAAY,EAAC,WAAW,CAAC;aACjD;YACD,UAAU,EAAE,OAAO,CAAC,QAAQ,KAAK,UAAU,CAAC;SAC/C,CAAC,CAAA;QAEF,OAAO,YAAY,CAAA;IAEvB,CAAC,CAAA;IAED,OAAO;QACH,YAAY;QACZ,eAAe,EAAf,uBAAe;QACf,cAAc;KACjB,CAAA;AAEL,CAAC,CAAA;AA3PY,QAAA,WAAW,eA2PvB","sourcesContent":["// import { QueryList, QueryGet } from \"@/schema/common\";\nimport { DefaultServiceProps, throwError } from \"@/utils\";\nimport {\n UserService,\n UserGetProps,\n UserType,\n UserListProps,\n userTypes,\n AccountService,\n LocationService,\n LocationListProps\n} from \"@/services\";\n\nimport jwt, { JwtPayload } from \"jsonwebtoken\";\nimport { config } from \"@/configs/config\";\n\nimport { generateAuthToken, verifyPassword, verifyAuthToken, hashPassword } from \"@/utils\"\nimport { ModelAccountFields } from \"@/models\";\n\nexport const authTypes = {\n operator: \"operator\",\n administrator: \"administrator\",\n worker: \"worker\",\n customer: \"customer\",\n} as const\n\nexport type AuthType = typeof authTypes[keyof typeof authTypes]\n\nexport type AuthJwtToken = JwtPayload & {\n user?: { id: number, user_id: number, user_type: AuthType };\n account?: { id: number, account_id: number };\n};\n\nexport type AuthServiceProps = DefaultServiceProps & {\n secretKey: string,\n authType: AuthType\n};\n\nexport type AuthGetTokenProps = {\n authType: AuthType,\n accountId?: number,\n accountCode?: string,\n data:\n // get token\n {\n username: string,\n password: string\n }\n};\n\nexport type AuthChangePasswordProps = {\n authToken: string,\n currentPassword: string\n newPassword: string\n confirmPassword: string\n};\n\nexport const AuthService = (props: AuthServiceProps) => {\n\n const {\n secretKey,\n authType = \"administrator\",\n // prisma,\n // authType = \"administrator\",\n // accountId = 0,\n // actionUserId = 0,\n ...rest\n } = props\n\n const getUserInjectionFieldName = (authType: AuthType) => {\n\n if (authType === \"administrator\") return [\"administrator_injection_fields\"]\n if (authType === \"customer\") return [\"customer_injection_fields\"]\n if (authType === \"worker\") return [\"worker_injection_fields\"]\n\n return []\n }\n\n const getAuthToken = async ({ authType, data, accountId = 0, accountCode = \"\" }: AuthGetTokenProps) => {\n\n let _rest = rest\n\n // check the option pass in using account id or account code\n if (authType !== \"operator\") {\n // _rest.accountId = accountId\n\n // check if the accountId & accountCode is not empty\n if (!accountId && !accountCode) throwError(`accountId or accountCode is required`)\n\n let selectAccountId = `${accountId}`\n\n // check account id is choose \n if (selectAccountId === \"0\") {\n\n const globalAccountService = AccountService({ ...rest, isOperator: true })\n\n // use account code to find the account id\n if (accountCode) {\n\n let { data: responseAcc } = await globalAccountService.listAccounts({\n limit: 1,\n offset: 0,\n sortfield: `${ModelAccountFields.account_id}`,\n sortorder: `ASC`,\n filters: [{\n field: `${ModelAccountFields.account_code}`,\n operator: `=`,\n value: `${accountCode}`,\n }],\n datatypes: []\n })\n\n selectAccountId = responseAcc.length > 0 ? responseAcc[0].account_id : \"\"\n let selectAccountStatus = responseAcc.length > 0 ? responseAcc[0].account_status : \"\"\n\n if (selectAccountStatus !== true) throwError(`Inactive account: ${accountCode}`)\n\n }\n\n if (!selectAccountId) {\n throwError(`Invalid account code: ${accountCode}`)\n }\n\n }\n\n _rest.accountId = Number(selectAccountId)\n\n } else {\n _rest.accountId = accountId\n }\n\n const userService = UserService({ userType: authType as UserType, ...rest })\n const accountService = AccountService({ ...rest, isOperator: true })\n const locationService = LocationService({ ...rest, locationType: \"location\" })\n\n // let data: any = {}\n\n let payload: UserListProps = {\n includePassword: true,\n limit: 1,\n offset: 0,\n sortfield: \"user_id\",\n sortorder: \"ASC\",\n filters: [\n { field: \"username\", operator: \"=\", value: `${data[\"username\"]}` },\n { field: \"user_type\", operator: \"=\", value: `${authType}` }\n ],\n datatypes: getUserInjectionFieldName(authType)\n }\n\n const { data: foundUser } = await userService.listUsers(payload)\n\n if (foundUser.length <= 0) {\n throw Error(`Invalid username: ${data[\"username\"]}`)\n }\n\n let user = foundUser.length > 0 ? foundUser[0] : {}\n\n let decryptedPass: boolean\n\n try {\n decryptedPass = await verifyPassword(data.password, user.password)\n } catch (error) {\n throw error\n }\n\n if (decryptedPass === false) {\n throw Error(`Invalid credential: ${data[\"username\"]}`)\n }\n\n // dont show out to result\n delete user.password\n\n let account: any = {}\n\n if (_rest.accountId && authType !== \"operator\") {\n account = await accountService.getAccount({ id: Number(_rest.accountId), datatypes: [] })\n }\n\n const authToken = generateAuthToken(user, account, secretKey)\n\n // get the locations for user\n const locationIds = user?.location_id ? user.location_id.split(\", \") : []\n\n let locations: any[] = []\n\n if (locationIds.length > 0) {\n const listPayload: LocationListProps = {\n limit: 100,\n offset: 0,\n sortfield: \"location_id\",\n sortorder: \"ASC\",\n filters: [{\n field: \"location_id\",\n operator: \"where_in\",\n value: locationIds.map((i: any) => Number(i))\n }],\n datatypes: []\n }\n\n const { data } = await locationService.listLocations(listPayload)\n locations = data\n }\n\n return {\n token: authToken,\n account,\n user,\n user_locations: locations,\n parent_user_id: 0,\n roles: [\n // {\n // id: 23,\n // role_name: \"123\"\n // }\n ],\n permissions: [],\n is_owner: Boolean(user?.is_owner === 1),\n owner_id: 0\n }\n };\n\n const changePassword = async ({ authToken, currentPassword, confirmPassword, newPassword }: AuthChangePasswordProps) => {\n\n const authSecretKey = config.AUTH_SECRET_KEY\n\n // validate the token\n const jwtData = verifyAuthToken(authToken, authSecretKey);\n\n const accountId = jwtData?.account?.account_id ?? \"\";\n const userId = jwtData?.user?.user_id ?? \"\";\n const userType = jwtData?.user?.user_type ?? \"\";\n\n if (!accountId && userType !== \"operator\") { // operator token do not have account id\n throwError(`Invalid or missing account id in auth token`)\n }\n\n if (!userId) {\n throwError(`Missing user id in auth token`)\n }\n\n if (!userType) {\n throwError(`Missing user type in auth token`)\n }\n\n const userService = UserService({ userType, ...rest })\n const accountService = AccountService({ ...rest, isOperator: true })\n\n const user = await userService.getUser({ id: Number(userId), datatypes: [], includePassword: true })\n const account = await accountService.getAccount({ id: Number(accountId), datatypes: [] })\n\n // console.log('jwtData', jwtData)\n // console.log('user', user)\n // console.log('account', account)\n\n // const a = await hashPassword(user?.password)\n\n // check the accountId / userId is valid\n if (!user?.user_id) {\n throwError(`Invalid user id in auth token`)\n }\n\n if (!account?.account_id && userType !== \"operator\") { // operator token do not have account id\n throwError(`Invalid account id in auth token`)\n }\n\n // new password cannot same with current password\n if (newPassword === currentPassword) {\n throwError(`New and current password cannot be same`)\n }\n\n // new password must same with confirm\n if (newPassword !== confirmPassword) {\n throwError(`New and confirm password not matched`)\n }\n\n // check the current password\n try {\n const decryptedPass = await verifyPassword(currentPassword, user?.password)\n // console.log('decryptedPass', decryptedPass)\n\n if (!decryptedPass) {\n throw Error(`Invalid current password`)\n }\n\n } catch (error: any) {\n throwError(`Auth Token: ${error?.message}`)\n }\n\n // perform update credential\n const responseUser = await userService.replacePassword({\n id: Number(userId),\n data: {\n loginPassword: await hashPassword(newPassword)\n },\n isOperator: Boolean(userType === \"operator\")\n })\n\n return responseUser\n\n }\n\n return {\n getAuthToken,\n verifyAuthToken,\n changePassword\n }\n\n}\n\n"]}
|
|
@@ -98,6 +98,7 @@ export type UserReplacePasswordProps = {
|
|
|
98
98
|
data: {
|
|
99
99
|
loginPassword: string;
|
|
100
100
|
};
|
|
101
|
+
isOperator: boolean;
|
|
101
102
|
};
|
|
102
103
|
export declare const UserService: (props: UserServiceProps) => {
|
|
103
104
|
createUser: ({ data, userType: overrideUserType }: UserCreateProps) => Promise<any>;
|
|
@@ -108,6 +109,6 @@ export declare const UserService: (props: UserServiceProps) => {
|
|
|
108
109
|
total: number;
|
|
109
110
|
}>;
|
|
110
111
|
removeUsers: ({ ids }: UserRemoveProps) => Promise<any[]>;
|
|
111
|
-
replacePassword: ({ id, data }: UserReplacePasswordProps) => Promise<any>;
|
|
112
|
+
replacePassword: ({ id, data, isOperator }: UserReplacePasswordProps) => Promise<any>;
|
|
112
113
|
getUserDataTypes: () => any[];
|
|
113
114
|
};
|
|
@@ -306,9 +306,13 @@ const UserService = (props) => {
|
|
|
306
306
|
});
|
|
307
307
|
return response;
|
|
308
308
|
};
|
|
309
|
-
const replacePassword = async ({ id, data }) => {
|
|
309
|
+
const replacePassword = async ({ id, data, isOperator = false }) => {
|
|
310
|
+
const passwordUserModel = (0, models_1.UserModel)({
|
|
311
|
+
...rest,
|
|
312
|
+
...(isOperator === true ? { isOperator } : {})
|
|
313
|
+
});
|
|
310
314
|
// update user password
|
|
311
|
-
let responseUser = await
|
|
315
|
+
let responseUser = await passwordUserModel.update({
|
|
312
316
|
id,
|
|
313
317
|
data: {
|
|
314
318
|
login_password: data?.loginPassword
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UserService.js","sourceRoot":"","sources":["../../../src/services/user/UserService.ts"],"names":[],"mappings":";;;AAAA,qCAGkB;AAElB,uDAA0E;AAC1E,uDAAmI;AACnI,iEAAiK;AACjK,mDAAuH;AACvH,uDAAmI;AAInI,mCAA0D;AAE1D,mCAAuC;AACvC,uDAAmI;AAEtH,QAAA,SAAS,GAAG;IACrB,QAAQ,EAAE,UAAU;IACpB,aAAa,EAAE,eAAe;IAC9B,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;CACV,CAAA;AAyIV,MAAM,gBAAgB,GAAG,KAAK,EAAE,QAAkB,EAAE,IAA6B,EAAE,EAAE;IAEjF,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;QAEjB,4BAA4B;QAE5B,OAAO;YACH,CAAC,GAAG,wBAAe,CAAC,cAAc,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa;YAC9D,CAAC,GAAG,wBAAe,CAAC,cAAc,EAAE,CAAC,EAAE,MAAM,IAAA,oBAAY,EAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC;YAClF,CAAC,GAAG,wBAAe,CAAC,OAAO,EAAE,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,EAAE;gBACF,cAAc,EAAE,IAAI,EAAE,aAAa;aACtC;SACJ,CAAC,CAAA;QAEF,IAAI,YAAY,GAAG,YAAY,CAAC,OAAO,CAAA;QAEvC,IAAI,QAAQ,GAAG,MAAM,OAAO,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,YAAY,CAAC,EAAE,eAAe,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAA;QAEjG,OAAO,QAAQ,CAAA;IACnB,CAAC,CAAA;IAED,MAAM,gBAAgB,GAAG,GAAG,EAAE;QAE1B,IAAI,IAAI,GAAU,EAAE,CAAC;QACrB,IAAI,KAAK,GAAG,CAAC,CAAC;QAEd,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC1B,IAAI,GAAG,eAAe,CAAC,YAAY,EAAE,CAAA;QACzC,CAAC;QAED,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC1B,IAAI,GAAG,eAAe,CAAC,YAAY,EAAE,CAAA;QACzC,CAAC;QAED,IAAI,QAAQ,KAAK,eAAe,EAAE,CAAC;YAC/B,IAAI,GAAG,oBAAoB,CAAC,YAAY,EAAE,CAAA;QAC9C,CAAC;QAED,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;YACxB,IAAI,GAAG,aAAa,CAAC,YAAY,EAAE,CAAA;QACvC,CAAC;QAED,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC1B,IAAI,GAAG,eAAe,CAAC,YAAY,EAAE,CAAA;QACzC,CAAC;QAED,OAAO,IAAI,CAAA;IACf,CAAC,CAAC;IAEF,OAAO;QACH,UAAU;QACV,UAAU;QACV,OAAO;QACP,SAAS;QACT,WAAW;QACX,eAAe;QACf,gBAAgB;KACnB,CAAA;AAEL,CAAC,CAAA;AAhaY,QAAA,WAAW,eAgavB","sourcesContent":["import {\n UserModel,\n ModelUserFields\n} from \"@/models\";\n\nimport { UserPropService, UserPropReplaceProps } from \"./UserPropService\";\nimport { CustomerGetProps, CustomerCreateProps, CustomerListProps, CustomerUpdateProps, CustomerService } from \"./CustomerService\";\nimport { AdministratorGetProps, AdministratorCreateProps, AdministratorUpdateProps, AdministratorListProps, AdministratorService } from \"./AdministratorService\";\nimport { WorkerCreateProps, WorkerGetProps, WorkerUpdateProps, WorkerListProps, WorkerService } from \"./WorkerService\";\nimport { RetailerCreateProps, RetailerGetProps, RetailerUpdateProps, RetailerListProps, RetailerService } from \"./RetailerService\";\n\nimport { QueryList, QueryGet } from \"@/schema/common\";\n\nimport { DefaultServiceProps, throwError } from \"@/utils\";\n\nimport { hashPassword } from \"@/utils\";\nimport { OperatorCreateProps, OperatorGetProps, OperatorListProps, OperatorService, OperatorUpdateProps } from \"./OperatorService\";\n\nexport const userTypes = {\n operator: \"operator\",\n administrator: \"administrator\",\n worker: \"worker\",\n supplier: \"supplier\",\n customer: \"customer\",\n driver: \"driver\",\n retailer: \"retailer\",\n member: \"member\",\n} as const\n\nexport type UserType = typeof userTypes[keyof typeof userTypes]\n\nexport type UserServiceProps = DefaultServiceProps & {\n userType: UserType\n};\n\nexport type UserGetProps = QueryGet & {\n id: number,\n includePassword?: boolean\n};\n\nexport type UserListProps = QueryList & {\n // id: number,\n includePassword?: boolean\n};\n\nexport type UserRemoveProps = {\n ids: BigInt[]\n};\n\nexport type UserCreateProps = {\n userType?: UserType,\n data: {\n user: {\n loginUsername: string\n loginPassword: string\n address: string\n contact: string\n email: string\n firstname: string\n lastname: string\n status: boolean\n }\n } & (\n // operator\n {\n operator: Omit<OperatorCreateProps[\"data\"][\"operator\"], \"userId\">,\n } |\n // customer\n {\n customer: Omit<CustomerCreateProps[\"data\"][\"customer\"], \"userId\">,\n } |\n // administrator\n {\n administrator: Omit<AdministratorCreateProps[\"data\"][\"administrator\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // worker\n {\n worker: Omit<WorkerCreateProps[\"data\"][\"worker\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // retailer\n {\n retailer: Omit<RetailerCreateProps[\"data\"][\"retailer\"], \"userId\">\n // props: {\n // locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n // }\n }\n )\n // CustomerCreateProps[\"data\"] |\n // // state\n // (StateCreateProps[\"data\"] & {\n // areas: AreaCreateProps[\"data\"][\"area\"][]\n // }) |\n // // area\n // (AreaCreateProps[\"data\"])\n}\n\nexport type UserUpdateProps = {\n id: number,\n userType?: UserType,\n data: {\n user: {\n loginUsername: string\n loginPassword?: string\n address: string\n contact: string\n email: string\n firstname: string\n lastname: string\n status: boolean\n }\n } & (\n // operator\n {\n operator: Omit<OperatorUpdateProps[\"data\"][\"operator\"], \"userId\">\n } |\n // customer\n {\n customer: Omit<CustomerUpdateProps[\"data\"][\"customer\"], \"userId\">\n } |\n // administrator\n {\n administrator: Omit<AdministratorUpdateProps[\"data\"][\"administrator\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // worker\n {\n worker: Omit<WorkerUpdateProps[\"data\"][\"worker\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // retailer\n {\n retailer: Omit<RetailerUpdateProps[\"data\"][\"retailer\"], \"userId\">\n // props: {\n // locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n // }\n }\n )\n // administrator\n // CustomerCreateProps[\"data\"] |\n // // state\n // (StateCreateProps[\"data\"] & {\n // areas: AreaCreateProps[\"data\"][\"area\"][]\n // }) |\n // // area\n // (AreaCreateProps[\"data\"])\n}\n\nexport type UserReplacePasswordProps = {\n id: number,\n data: {\n loginPassword: string\n }\n}\n\nconst getCreatePayload = async (userType: UserType, data: UserCreateProps[\"data\"]) => {\n\n if (\"user\" in data) {\n\n // have to hash the password\n\n return {\n [`${ModelUserFields.login_username}`]: data.user.loginUsername,\n [`${ModelUserFields.login_password}`]: await hashPassword(data.user.loginPassword),\n [`${ModelUserFields.address}`]: data.user.address,\n [`${ModelUserFields.contact}`]: data.user.contact,\n [`${ModelUserFields.email}`]: data.user.email,\n [`${ModelUserFields.firstname}`]: data.user.firstname,\n [`${ModelUserFields.lastname}`]: data.user.lastname,\n [`${ModelUserFields.status}`]: data.user.status ? 1 : 0,\n [`${ModelUserFields.user_type}`]: `${userType}`\n }\n }\n\n return false\n\n}\n\nconst getUpdatePayload = (userType: UserType, data: UserUpdateProps[\"data\"]) => {\n\n if (\"user\" in data) {\n\n return {\n [`${ModelUserFields.login_username}`]: data.user.loginUsername,\n ...(data?.user?.loginPassword ? { [`${ModelUserFields.login_password}`]: data.user.loginPassword } : {}),\n [`${ModelUserFields.address}`]: data.user.address,\n [`${ModelUserFields.contact}`]: data.user.contact,\n [`${ModelUserFields.email}`]: data.user.email,\n [`${ModelUserFields.firstname}`]: data.user.firstname,\n [`${ModelUserFields.lastname}`]: data.user.lastname,\n [`${ModelUserFields.status}`]: data.user.status ? 1 : 0,\n [`${ModelUserFields.user_type}`]: `${userType}`\n }\n }\n\n return false\n\n}\n\nexport const UserService = (props: UserServiceProps) => {\n\n const {\n // prisma,\n userType = \"administrator\",\n ...rest\n // accountId = 0,\n // actionUserId = 0,\n } = props\n\n const userModel = UserModel({ ...rest })\n\n const userPropService = UserPropService({ ...rest, userPropType: \"LOCATION_ID\" })\n const customerService = CustomerService({ ...rest })\n const administratorService = AdministratorService({ ...rest })\n const operatorService = OperatorService({ ...rest })\n const workerService = WorkerService({ ...rest })\n const retailerService = RetailerService({ ...rest })\n\n const createUser = async ({ data, userType: overrideUserType }: UserCreateProps) => {\n\n const _userType = overrideUserType || userType\n\n // check the username duplicated?\n\n const _data = await getCreatePayload(_userType, data)\n\n if (!_data) {\n throw new Error(`Invalid create payload: ${JSON.stringify(_data)}`);\n }\n\n // create user\n let responseUser = await userModel.create({ data: _data })\n\n let newUserId = responseUser.user_id\n\n let response: any = {}\n\n if (_userType === \"customer\" && \"customer\" in data) {\n\n let dataCustomer: CustomerCreateProps[\"data\"] = {\n customer: {\n ...data.customer,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create customer\n await customerService.createCustomer({ data: dataCustomer })\n\n response = await customerService.getCustomer({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"administrator\" && \"administrator\" in data) {\n\n let dataAdministrator: AdministratorCreateProps[\"data\"] = {\n administrator: {\n ...data.administrator,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create customer\n await administratorService.createAdministrator({ data: dataAdministrator })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(newUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await administratorService.getAdministrator({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"operator\" && \"operator\" in data) {\n\n let dataOperator: OperatorCreateProps[\"data\"] = {\n operator: {\n ...data.operator,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create operator\n await operatorService.createOperator({ data: dataOperator })\n\n response = await operatorService.getOperator({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"worker\" && \"worker\" in data) {\n\n let dataWorker: WorkerCreateProps[\"data\"] = {\n worker: {\n ...data.worker,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create customer\n await workerService.createWorker({ data: dataWorker })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(newUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await workerService.getWorker({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"retailer\" && \"retailer\" in data) {\n\n let dataRetailer: RetailerCreateProps[\"data\"] = {\n retailer: {\n ...data.retailer,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create retailer\n await retailerService.createRetailer({ data: dataRetailer })\n\n response = await retailerService.getRetailer({ id: Number(newUserId), datatypes: [] })\n }\n\n return response\n }\n\n const updateUser = async ({ id, data, userType: overrideUserType }: UserUpdateProps) => {\n\n const _userType = overrideUserType || userType\n\n const _data = getUpdatePayload(_userType, data)\n\n if (!_data) {\n throw new Error(`Invalid update payload: ${JSON.stringify(_data)}`);\n }\n\n // update user\n let responseUser = await userModel.update({ id, data: _data })\n\n let updateUserId = responseUser.user_id\n\n let response: any = {}\n\n if (_userType === \"customer\" && \"customer\" in data) {\n\n let dataCustomer: CustomerUpdateProps[\"data\"] = {\n customer: {\n ...data.customer\n }\n }\n\n await customerService.updateCustomer({ userId: Number(updateUserId), data: dataCustomer })\n\n response = await customerService.getCustomer({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"operator\" && \"operator\" in data) {\n\n let dataOperator: OperatorUpdateProps[\"data\"] = {\n operator: {\n ...data.operator\n }\n }\n\n await operatorService.updateOperator({ userId: Number(updateUserId), data: dataOperator })\n\n response = await operatorService.getOperator({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"administrator\" && \"administrator\" in data) {\n\n let dataCustomer: AdministratorUpdateProps[\"data\"] = {\n administrator: {\n ...data.administrator\n }\n }\n\n await administratorService.updateAdministrator({ userId: Number(updateUserId), data: dataCustomer })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(updateUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await administratorService.getAdministrator({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"worker\" && \"worker\" in data) {\n\n let dataWorker: WorkerUpdateProps[\"data\"] = {\n worker: {\n ...data.worker\n }\n }\n\n await workerService.updateWorker({ userId: Number(updateUserId), data: dataWorker })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(updateUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await workerService.getWorker({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"retailer\" && \"retailer\" in data) {\n\n let dataRetailer: RetailerUpdateProps[\"data\"] = {\n retailer: {\n ...data.retailer\n }\n }\n\n await retailerService.updateRetailer({ userId: Number(updateUserId), data: dataRetailer })\n\n response = await retailerService.getRetailer({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n return response\n }\n\n const getUser = async ({ id, datatypes = [], includePassword = false }: UserGetProps) => {\n\n let data: any = {}\n\n if (userType === \"customer\") {\n data = await customerService.getCustomer({ id, datatypes: datatypes as CustomerGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"operator\") {\n data = await operatorService.getOperator({ id, datatypes: datatypes as OperatorGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"administrator\") {\n data = await administratorService.getAdministrator({ id, datatypes: datatypes as AdministratorGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"worker\") {\n data = await workerService.getWorker({ id, datatypes: datatypes as WorkerGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"retailer\") {\n data = await retailerService.getRetailer({ id, datatypes: datatypes as RetailerGetProps[\"datatypes\"], includePassword })\n }\n\n if (!data) {\n throw new Error(`Invalid id: ${id}`);\n }\n\n return data\n };\n\n const listUsers = async ({ limit, offset, filters, sortfield, sortorder, datatypes, includePassword = false }: UserListProps) => {\n\n let data: any[] = [];\n let total = 0;\n\n if (userType === \"customer\") {\n ({ data, total } = await customerService.listCustomers(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as CustomerListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"operator\") {\n ({ data, total } = await operatorService.listOperators(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as OperatorListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"administrator\") {\n ({ data, total } = await administratorService.listAdministrators(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as AdministratorListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"worker\") {\n ({ data, total } = await workerService.listWorkers(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as WorkerListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"retailer\") {\n ({ data, total } = await retailerService.listRetailers(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as RetailerListProps[\"datatypes\"] }\n ))\n }\n\n return {\n data,\n total\n }\n };\n\n const removeUsers = async ({ ids }: UserRemoveProps) => {\n\n if (!ids.length) throwError('id is required');\n\n let response: any[] = []\n\n if (userType === \"customer\") {\n response = await customerService.removeCustomers({ userIds: ids })\n }\n\n if (userType === \"operator\") {\n response = await operatorService.removeOperators({ userIds: ids })\n }\n\n if (userType === \"administrator\") {\n response = await administratorService.removeAdministrators({ userIds: ids })\n await userPropService.removeUserProps({ userIds: ids })\n }\n\n if (userType === \"worker\") {\n response = await workerService.removeWorkers({ userIds: ids })\n await userPropService.removeUserProps({ userIds: ids })\n }\n\n if (userType === \"retailer\") {\n response = await retailerService.removeRetailers({ userIds: ids })\n // await userPropService.removeUserProps({ userIds: ids })\n }\n\n // remove user\n let responseUser = await userModel.remove({ ids: ids.map(i => Number(i)) })\n\n response.forEach((i, index) => {\n i.user = responseUser[index]\n })\n\n return response\n };\n\n const replacePassword = async ({ id, data }: UserReplacePasswordProps) => {\n\n // update user password\n let responseUser = await userModel.update({\n id,\n data: {\n login_password: data?.loginPassword\n }\n })\n\n let updateUserId = responseUser.user_id\n\n let response = await getUser({ id: Number(updateUserId), includePassword: false, datatypes: [] })\n\n return response\n }\n\n const getUserDataTypes = () => {\n\n let data: any[] = [];\n let total = 0;\n\n if (userType === \"customer\") {\n data = customerService.getDataTypes()\n }\n\n if (userType === \"operator\") {\n data = operatorService.getDataTypes()\n }\n\n if (userType === \"administrator\") {\n data = administratorService.getDataTypes()\n }\n\n if (userType === \"worker\") {\n data = workerService.getDataTypes()\n }\n\n if (userType === \"retailer\") {\n data = retailerService.getDataTypes()\n }\n\n return data\n };\n\n return {\n createUser,\n updateUser,\n getUser,\n listUsers,\n removeUsers,\n replacePassword,\n getUserDataTypes\n }\n\n}\n\n"]}
|
|
1
|
+
{"version":3,"file":"UserService.js","sourceRoot":"","sources":["../../../src/services/user/UserService.ts"],"names":[],"mappings":";;;AAAA,qCAGkB;AAElB,uDAA0E;AAC1E,uDAAmI;AACnI,iEAAiK;AACjK,mDAAuH;AACvH,uDAAmI;AAInI,mCAA0D;AAE1D,mCAAuC;AACvC,uDAAmI;AAEtH,QAAA,SAAS,GAAG;IACrB,QAAQ,EAAE,UAAU;IACpB,aAAa,EAAE,eAAe;IAC9B,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;CACV,CAAA;AA0IV,MAAM,gBAAgB,GAAG,KAAK,EAAE,QAAkB,EAAE,IAA6B,EAAE,EAAE;IAEjF,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;QAEjB,4BAA4B;QAE5B,OAAO;YACH,CAAC,GAAG,wBAAe,CAAC,cAAc,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa;YAC9D,CAAC,GAAG,wBAAe,CAAC,cAAc,EAAE,CAAC,EAAE,MAAM,IAAA,oBAAY,EAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC;YAClF,CAAC,GAAG,wBAAe,CAAC,OAAO,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO;YACjD,CAAC,GAAG,wBAAe,CAAC,OAAO,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO;YACjD,CAAC,GAAG,wBAAe,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK;YAC7C,CAAC,GAAG,wBAAe,CAAC,SAAS,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI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AAI,YAAY,GAAG,YAAY,CAAC,OAAO,CAAA;QAEvC,IAAI,QAAQ,GAAG,MAAM,OAAO,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,YAAY,CAAC,EAAE,eAAe,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAA;QAEjG,OAAO,QAAQ,CAAA;IACnB,CAAC,CAAA;IAED,MAAM,gBAAgB,GAAG,GAAG,EAAE;QAE1B,IAAI,IAAI,GAAU,EAAE,CAAC;QACrB,IAAI,KAAK,GAAG,CAAC,CAAC;QAEd,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC1B,IAAI,GAAG,eAAe,CAAC,YAAY,EAAE,CAAA;QACzC,CAAC;QAED,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC1B,IAAI,GAAG,eAAe,CAAC,YAAY,EAAE,CAAA;QACzC,CAAC;QAED,IAAI,QAAQ,KAAK,eAAe,EAAE,CAAC;YAC/B,IAAI,GAAG,oBAAoB,CAAC,YAAY,EAAE,CAAA;QAC9C,CAAC;QAED,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;YACxB,IAAI,GAAG,aAAa,CAAC,YAAY,EAAE,CAAA;QACvC,CAAC;QAED,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC1B,IAAI,GAAG,eAAe,CAAC,YAAY,EAAE,CAAA;QACzC,CAAC;QAED,OAAO,IAAI,CAAA;IACf,CAAC,CAAC;IAEF,OAAO;QACH,UAAU;QACV,UAAU;QACV,OAAO;QACP,SAAS;QACT,WAAW;QACX,eAAe;QACf,gBAAgB;KACnB,CAAA;AAEL,CAAC,CAAA;AAraY,QAAA,WAAW,eAqavB","sourcesContent":["import {\n UserModel,\n ModelUserFields\n} from \"@/models\";\n\nimport { UserPropService, UserPropReplaceProps } from \"./UserPropService\";\nimport { CustomerGetProps, CustomerCreateProps, CustomerListProps, CustomerUpdateProps, CustomerService } from \"./CustomerService\";\nimport { AdministratorGetProps, AdministratorCreateProps, AdministratorUpdateProps, AdministratorListProps, AdministratorService } from \"./AdministratorService\";\nimport { WorkerCreateProps, WorkerGetProps, WorkerUpdateProps, WorkerListProps, WorkerService } from \"./WorkerService\";\nimport { RetailerCreateProps, RetailerGetProps, RetailerUpdateProps, RetailerListProps, RetailerService } from \"./RetailerService\";\n\nimport { QueryList, QueryGet } from \"@/schema/common\";\n\nimport { DefaultServiceProps, throwError } from \"@/utils\";\n\nimport { hashPassword } from \"@/utils\";\nimport { OperatorCreateProps, OperatorGetProps, OperatorListProps, OperatorService, OperatorUpdateProps } from \"./OperatorService\";\n\nexport const userTypes = {\n operator: \"operator\",\n administrator: \"administrator\",\n worker: \"worker\",\n supplier: \"supplier\",\n customer: \"customer\",\n driver: \"driver\",\n retailer: \"retailer\",\n member: \"member\",\n} as const\n\nexport type UserType = typeof userTypes[keyof typeof userTypes]\n\nexport type UserServiceProps = DefaultServiceProps & {\n userType: UserType\n};\n\nexport type UserGetProps = QueryGet & {\n id: number,\n includePassword?: boolean\n};\n\nexport type UserListProps = QueryList & {\n // id: number,\n includePassword?: boolean\n};\n\nexport type UserRemoveProps = {\n ids: BigInt[]\n};\n\nexport type UserCreateProps = {\n userType?: UserType,\n data: {\n user: {\n loginUsername: string\n loginPassword: string\n address: string\n contact: string\n email: string\n firstname: string\n lastname: string\n status: boolean\n }\n } & (\n // operator\n {\n operator: Omit<OperatorCreateProps[\"data\"][\"operator\"], \"userId\">,\n } |\n // customer\n {\n customer: Omit<CustomerCreateProps[\"data\"][\"customer\"], \"userId\">,\n } |\n // administrator\n {\n administrator: Omit<AdministratorCreateProps[\"data\"][\"administrator\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // worker\n {\n worker: Omit<WorkerCreateProps[\"data\"][\"worker\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // retailer\n {\n retailer: Omit<RetailerCreateProps[\"data\"][\"retailer\"], \"userId\">\n // props: {\n // locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n // }\n }\n )\n // CustomerCreateProps[\"data\"] |\n // // state\n // (StateCreateProps[\"data\"] & {\n // areas: AreaCreateProps[\"data\"][\"area\"][]\n // }) |\n // // area\n // (AreaCreateProps[\"data\"])\n}\n\nexport type UserUpdateProps = {\n id: number,\n userType?: UserType,\n data: {\n user: {\n loginUsername: string\n loginPassword?: string\n address: string\n contact: string\n email: string\n firstname: string\n lastname: string\n status: boolean\n }\n } & (\n // operator\n {\n operator: Omit<OperatorUpdateProps[\"data\"][\"operator\"], \"userId\">\n } |\n // customer\n {\n customer: Omit<CustomerUpdateProps[\"data\"][\"customer\"], \"userId\">\n } |\n // administrator\n {\n administrator: Omit<AdministratorUpdateProps[\"data\"][\"administrator\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // worker\n {\n worker: Omit<WorkerUpdateProps[\"data\"][\"worker\"], \"userId\">\n props: {\n locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n }\n } |\n // retailer\n {\n retailer: Omit<RetailerUpdateProps[\"data\"][\"retailer\"], \"userId\">\n // props: {\n // locationIds: UserPropReplaceProps[\"data\"][\"userProp\"][\"locationIds\"]\n // }\n }\n )\n // administrator\n // CustomerCreateProps[\"data\"] |\n // // state\n // (StateCreateProps[\"data\"] & {\n // areas: AreaCreateProps[\"data\"][\"area\"][]\n // }) |\n // // area\n // (AreaCreateProps[\"data\"])\n}\n\nexport type UserReplacePasswordProps = {\n id: number,\n data: {\n loginPassword: string\n },\n isOperator: boolean\n}\n\nconst getCreatePayload = async (userType: UserType, data: UserCreateProps[\"data\"]) => {\n\n if (\"user\" in data) {\n\n // have to hash the password\n\n return {\n [`${ModelUserFields.login_username}`]: data.user.loginUsername,\n [`${ModelUserFields.login_password}`]: await hashPassword(data.user.loginPassword),\n [`${ModelUserFields.address}`]: data.user.address,\n [`${ModelUserFields.contact}`]: data.user.contact,\n [`${ModelUserFields.email}`]: data.user.email,\n [`${ModelUserFields.firstname}`]: data.user.firstname,\n [`${ModelUserFields.lastname}`]: data.user.lastname,\n [`${ModelUserFields.status}`]: data.user.status ? 1 : 0,\n [`${ModelUserFields.user_type}`]: `${userType}`\n }\n }\n\n return false\n\n}\n\nconst getUpdatePayload = (userType: UserType, data: UserUpdateProps[\"data\"]) => {\n\n if (\"user\" in data) {\n\n return {\n [`${ModelUserFields.login_username}`]: data.user.loginUsername,\n ...(data?.user?.loginPassword ? { [`${ModelUserFields.login_password}`]: data.user.loginPassword } : {}),\n [`${ModelUserFields.address}`]: data.user.address,\n [`${ModelUserFields.contact}`]: data.user.contact,\n [`${ModelUserFields.email}`]: data.user.email,\n [`${ModelUserFields.firstname}`]: data.user.firstname,\n [`${ModelUserFields.lastname}`]: data.user.lastname,\n [`${ModelUserFields.status}`]: data.user.status ? 1 : 0,\n [`${ModelUserFields.user_type}`]: `${userType}`\n }\n }\n\n return false\n\n}\n\nexport const UserService = (props: UserServiceProps) => {\n\n const {\n // prisma,\n userType = \"administrator\",\n ...rest\n // accountId = 0,\n // actionUserId = 0,\n } = props\n\n const userModel = UserModel({ ...rest })\n\n const userPropService = UserPropService({ ...rest, userPropType: \"LOCATION_ID\" })\n const customerService = CustomerService({ ...rest })\n const administratorService = AdministratorService({ ...rest })\n const operatorService = OperatorService({ ...rest })\n const workerService = WorkerService({ ...rest })\n const retailerService = RetailerService({ ...rest })\n\n const createUser = async ({ data, userType: overrideUserType }: UserCreateProps) => {\n\n const _userType = overrideUserType || userType\n\n // check the username duplicated?\n\n const _data = await getCreatePayload(_userType, data)\n\n if (!_data) {\n throw new Error(`Invalid create payload: ${JSON.stringify(_data)}`);\n }\n\n // create user\n let responseUser = await userModel.create({ data: _data })\n\n let newUserId = responseUser.user_id\n\n let response: any = {}\n\n if (_userType === \"customer\" && \"customer\" in data) {\n\n let dataCustomer: CustomerCreateProps[\"data\"] = {\n customer: {\n ...data.customer,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create customer\n await customerService.createCustomer({ data: dataCustomer })\n\n response = await customerService.getCustomer({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"administrator\" && \"administrator\" in data) {\n\n let dataAdministrator: AdministratorCreateProps[\"data\"] = {\n administrator: {\n ...data.administrator,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create customer\n await administratorService.createAdministrator({ data: dataAdministrator })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(newUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await administratorService.getAdministrator({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"operator\" && \"operator\" in data) {\n\n let dataOperator: OperatorCreateProps[\"data\"] = {\n operator: {\n ...data.operator,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create operator\n await operatorService.createOperator({ data: dataOperator })\n\n response = await operatorService.getOperator({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"worker\" && \"worker\" in data) {\n\n let dataWorker: WorkerCreateProps[\"data\"] = {\n worker: {\n ...data.worker,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create customer\n await workerService.createWorker({ data: dataWorker })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(newUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await workerService.getWorker({ id: Number(newUserId), datatypes: [] })\n }\n\n if (_userType === \"retailer\" && \"retailer\" in data) {\n\n let dataRetailer: RetailerCreateProps[\"data\"] = {\n retailer: {\n ...data.retailer,\n userId: Number(newUserId) ?? \"\"\n }\n }\n\n // create retailer\n await retailerService.createRetailer({ data: dataRetailer })\n\n response = await retailerService.getRetailer({ id: Number(newUserId), datatypes: [] })\n }\n\n return response\n }\n\n const updateUser = async ({ id, data, userType: overrideUserType }: UserUpdateProps) => {\n\n const _userType = overrideUserType || userType\n\n const _data = getUpdatePayload(_userType, data)\n\n if (!_data) {\n throw new Error(`Invalid update payload: ${JSON.stringify(_data)}`);\n }\n\n // update user\n let responseUser = await userModel.update({ id, data: _data })\n\n let updateUserId = responseUser.user_id\n\n let response: any = {}\n\n if (_userType === \"customer\" && \"customer\" in data) {\n\n let dataCustomer: CustomerUpdateProps[\"data\"] = {\n customer: {\n ...data.customer\n }\n }\n\n await customerService.updateCustomer({ userId: Number(updateUserId), data: dataCustomer })\n\n response = await customerService.getCustomer({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"operator\" && \"operator\" in data) {\n\n let dataOperator: OperatorUpdateProps[\"data\"] = {\n operator: {\n ...data.operator\n }\n }\n\n await operatorService.updateOperator({ userId: Number(updateUserId), data: dataOperator })\n\n response = await operatorService.getOperator({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"administrator\" && \"administrator\" in data) {\n\n let dataCustomer: AdministratorUpdateProps[\"data\"] = {\n administrator: {\n ...data.administrator\n }\n }\n\n await administratorService.updateAdministrator({ userId: Number(updateUserId), data: dataCustomer })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(updateUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await administratorService.getAdministrator({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"worker\" && \"worker\" in data) {\n\n let dataWorker: WorkerUpdateProps[\"data\"] = {\n worker: {\n ...data.worker\n }\n }\n\n await workerService.updateWorker({ userId: Number(updateUserId), data: dataWorker })\n\n // replace user prop\n await userPropService.replaceUserProp({\n userPropType: \"LOCATION_ID\",\n data: {\n userProp: {\n userId: Number(updateUserId),\n locationIds: data.props.locationIds\n }\n }\n })\n\n response = await workerService.getWorker({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n if (_userType === \"retailer\" && \"retailer\" in data) {\n\n let dataRetailer: RetailerUpdateProps[\"data\"] = {\n retailer: {\n ...data.retailer\n }\n }\n\n await retailerService.updateRetailer({ userId: Number(updateUserId), data: dataRetailer })\n\n response = await retailerService.getRetailer({ id: Number(updateUserId), datatypes: [] })\n\n }\n\n return response\n }\n\n const getUser = async ({ id, datatypes = [], includePassword = false }: UserGetProps) => {\n\n let data: any = {}\n\n if (userType === \"customer\") {\n data = await customerService.getCustomer({ id, datatypes: datatypes as CustomerGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"operator\") {\n data = await operatorService.getOperator({ id, datatypes: datatypes as OperatorGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"administrator\") {\n data = await administratorService.getAdministrator({ id, datatypes: datatypes as AdministratorGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"worker\") {\n data = await workerService.getWorker({ id, datatypes: datatypes as WorkerGetProps[\"datatypes\"], includePassword })\n }\n\n if (userType === \"retailer\") {\n data = await retailerService.getRetailer({ id, datatypes: datatypes as RetailerGetProps[\"datatypes\"], includePassword })\n }\n\n if (!data) {\n throw new Error(`Invalid id: ${id}`);\n }\n\n return data\n };\n\n const listUsers = async ({ limit, offset, filters, sortfield, sortorder, datatypes, includePassword = false }: UserListProps) => {\n\n let data: any[] = [];\n let total = 0;\n\n if (userType === \"customer\") {\n ({ data, total } = await customerService.listCustomers(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as CustomerListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"operator\") {\n ({ data, total } = await operatorService.listOperators(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as OperatorListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"administrator\") {\n ({ data, total } = await administratorService.listAdministrators(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as AdministratorListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"worker\") {\n ({ data, total } = await workerService.listWorkers(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as WorkerListProps[\"datatypes\"] }\n ))\n }\n\n if (userType === \"retailer\") {\n ({ data, total } = await retailerService.listRetailers(\n { includePassword, limit, offset, filters, sortfield, sortorder, datatypes: datatypes as RetailerListProps[\"datatypes\"] }\n ))\n }\n\n return {\n data,\n total\n }\n };\n\n const removeUsers = async ({ ids }: UserRemoveProps) => {\n\n if (!ids.length) throwError('id is required');\n\n let response: any[] = []\n\n if (userType === \"customer\") {\n response = await customerService.removeCustomers({ userIds: ids })\n }\n\n if (userType === \"operator\") {\n response = await operatorService.removeOperators({ userIds: ids })\n }\n\n if (userType === \"administrator\") {\n response = await administratorService.removeAdministrators({ userIds: ids })\n await userPropService.removeUserProps({ userIds: ids })\n }\n\n if (userType === \"worker\") {\n response = await workerService.removeWorkers({ userIds: ids })\n await userPropService.removeUserProps({ userIds: ids })\n }\n\n if (userType === \"retailer\") {\n response = await retailerService.removeRetailers({ userIds: ids })\n // await userPropService.removeUserProps({ userIds: ids })\n }\n\n // remove user\n let responseUser = await userModel.remove({ ids: ids.map(i => Number(i)) })\n\n response.forEach((i, index) => {\n i.user = responseUser[index]\n })\n\n return response\n };\n\n const replacePassword = async ({ id, data, isOperator = false }: UserReplacePasswordProps) => {\n\n const passwordUserModel = UserModel({\n ...rest,\n ...(isOperator === true ? { isOperator } : {})\n })\n\n // update user password\n let responseUser = await passwordUserModel.update({\n id,\n data: {\n login_password: data?.loginPassword\n }\n })\n\n let updateUserId = responseUser.user_id\n\n let response = await getUser({ id: Number(updateUserId), includePassword: false, datatypes: [] })\n\n return response\n }\n\n const getUserDataTypes = () => {\n\n let data: any[] = [];\n let total = 0;\n\n if (userType === \"customer\") {\n data = customerService.getDataTypes()\n }\n\n if (userType === \"operator\") {\n data = operatorService.getDataTypes()\n }\n\n if (userType === \"administrator\") {\n data = administratorService.getDataTypes()\n }\n\n if (userType === \"worker\") {\n data = workerService.getDataTypes()\n }\n\n if (userType === \"retailer\") {\n data = retailerService.getDataTypes()\n }\n\n return data\n };\n\n return {\n createUser,\n updateUser,\n getUser,\n listUsers,\n removeUsers,\n replacePassword,\n getUserDataTypes\n }\n\n}\n\n"]}
|