storemw-core-api 1.0.138 → 1.0.139

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.
@@ -150,9 +150,9 @@ const AuthService = (props) => {
150
150
  // dont show out to result
151
151
  delete user.password;
152
152
  let account = {};
153
- if (_rest.accountId && authType !== "operator") {
154
- account = await accountService.getAccount({ id: Number(_rest.accountId), datatypes: [] });
155
- }
153
+ // if (_rest.accountId && authType !== "operator") {
154
+ account = await accountService.getAccount({ id: Number(_rest.accountId), datatypes: [] });
155
+ // }
156
156
  const authToken = (0, utils_2.generateAuthToken)(user, account, secretKey);
157
157
  // get the locations for user
158
158
  const locationIds = user?.location_id ? user.location_id.split(", ") : [];
@@ -1 +1 @@
1
- {"version":3,"file":"AuthService.js","sourceRoot":"","sources":["../../../src/services/auth/AuthService.ts"],"names":[],"mappings":";;;AAAA,yDAAyD;AACzD,mCAA0D;AAC1D,yCASoB;AAIpB,mCAA0F;AAC1F,qCAA0E;AAE7D,QAAA,SAAS,GAAG;IACrB,QAAQ,EAAE,UAAU;IACpB,aAAa,EAAE,eAAe;IAC9B,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;CACV,CAAA;AA0BH,MAAM,WAAW,GAAG,CAAC,KAAuB,EAAE,EAAE;IAEnD,MAAM,EACF,SAAS,EACT,QAAQ,GAAG,eAAe,EAC1B,GAAG,IAAI,EACV,GAAG,KAAK,CAAA;IAET,MAAM,yBAAyB,GAAG,CAAC,QAAkB,EAAE,EAAE;QAErD,IAAI,QAAQ,KAAK,eAAe;YAAE,OAAO,CAAC,gCAAgC,CAAC,CAAA;QAC3E,IAAI,QAAQ,KAAK,UAAU;YAAE,OAAO,CAAC,2BAA2B,CAAC,CAAA;QACjE,IAAI,QAAQ,KAAK,QAAQ;YAAE,OAAO,CAAC,yBAAyB,CAAC,CAAA;QAE7D,OAAO,EAAE,CAAA;IACb,CAAC,CAAA;IAED,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ChB,OAAO,EAAE,CAAC;wBACN,KAAK,EAAE,aAAa;wBACpB,QAAQ,EAAE,UAAU;wBACpB,KAAK,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;qBAChD,CAAC;gBACF,SAAS,EAAE,EAAE;aAChB,CAAA;YAED,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,eAAe,CAAC,aAAa,CAAC,WAAW,CAAC,CAAA;YACjE,SAAS,GAAG,IAAI,CAAA;QACpB,CAAC;QAED,OAAO;YACH,KAAK,EAAE,SAAS;YAChB,OAAO;YACP,IAAI;YACJ,cAAc,EAAE,SAAS;YACzB,qBAAqB;YACrB,WAAW;YACX,IAAI;YACJ,cAAc;YACd,uBAAuB;YACvB,IAAI;YACJ,KAAK;YACL,mBAAmB;YACnB,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;YACjC,cAAc;SACjB,CAAA;IACL,CAAC,CAAC;IAEF,OAAO;QACH,YAAY;QACZ,eAAe,EAAf,uBAAe;KAClB,CAAA;AAEL,CAAC,CAAA;AAnPY,QAAA,WAAW,eAmPvB","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\";\n\nimport { generateAuthToken, verifyPassword, verifyAuthToken, hashPassword } from \"@/utils\"\nimport { ModelAccountFields, ModelUserFields, UserModel } from \"@/models\";\n\nexport const authTypes = {\n operator: \"operator\",\n administrator: \"administrator\",\n worker: \"worker\",\n customer: \"customer\",\n member: \"member\",\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 const AuthService = (props: AuthServiceProps) => {\n\n const {\n secretKey,\n authType = \"administrator\",\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 validateAuthAccount = async (accountCode: string) => {\n\n if (accountCode === \"\") {\n return 0\n }\n\n // check if the accountId & accountCode is not empty\n // if (!accountId && !accountCode) throwError(`accountId or accountCode is required`)\n\n let selectAccountId = 0\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 return Number(selectAccountId ?? 0)\n\n }\n\n const getAuthToken = async ({ authType, data, accountCode = \"\" }: AuthGetTokenProps) => {\n\n let _rest = rest\n let _accountId = 0\n\n if (authType !== \"operator\") {\n _accountId = await validateAuthAccount(accountCode)\n }\n\n // _rest.accountId = accountId\n _rest.accountId = _accountId\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 foundUser: any = {}\n\n // only for operator & administrator\n if (authType === \"operator\" || authType === \"administrator\") {\n\n // const operatorService = UserService({ ...rest })\n\n const _user = await userService.getUserByUsername({\n username: `${data[\"username\"]}`,\n includePassword: true,\n datatypes: getUserInjectionFieldName(authType) as any\n })\n\n foundUser = _user\n\n } else { // other user type\n\n let payload: UserListProps = {\n includePassword: true,\n limit: 1,\n offset: 0,\n sortfield: \"user_id\",\n sortorder: \"DESC\",\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: foundOtherUser } = await userService.listUsers(payload)\n\n foundUser = foundOtherUser.length > 0 ? foundOtherUser[0] : {}\n\n }\n\n if (!foundUser?.user_id) {\n throw Error(`Invalid username: ${data[\"username\"]}`)\n }\n\n let user = foundUser\n // 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),\n // owner_id: 0\n }\n };\n\n return {\n getAuthToken,\n verifyAuthToken,\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;AAIpB,mCAA0F;AAC1F,qCAA0E;AAE7D,QAAA,SAAS,GAAG;IACrB,QAAQ,EAAE,UAAU;IACpB,aAAa,EAAE,eAAe;IAC9B,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;CACV,CAAA;AA0BH,MAAM,WAAW,GAA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AE7D,6BAA6B;QAC7B,MAAM,WAAW,GAAG,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;QAEzE,IAAI,SAAS,GAAU,EAAE,CAAA;QAEzB,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACzB,MAAM,WAAW,GAAsB;gBACnC,KAAK,EAAE,GAAG;gBACV,MAAM,EAAE,CAAC;gBACT,SAAS,EAAE,aAAa;gBACxB,SAAS,EAAE,KAAK;gBAChB,OAAO,EAAE,CAAC;wBACN,KAAK,EAAE,aAAa;wBACpB,QAAQ,EAAE,UAAU;wBACpB,KAAK,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;qBAChD,CAAC;gBACF,SAAS,EAAE,EAAE;aAChB,CAAA;YAED,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,eAAe,CAAC,aAAa,CAAC,WAAW,CAAC,CAAA;YACjE,SAAS,GAAG,IAAI,CAAA;QACpB,CAAC;QAED,OAAO;YACH,KAAK,EAAE,SAAS;YAChB,OAAO;YACP,IAAI;YACJ,cAAc,EAAE,SAAS;YACzB,qBAAqB;YACrB,WAAW;YACX,IAAI;YACJ,cAAc;YACd,uBAAuB;YACvB,IAAI;YACJ,KAAK;YACL,mBAAmB;YACnB,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;YACjC,cAAc;SACjB,CAAA;IACL,CAAC,CAAC;IAEF,OAAO;QACH,YAAY;QACZ,eAAe,EAAf,uBAAe;KAClB,CAAA;AAEL,CAAC,CAAA;AAnPY,QAAA,WAAW,eAmPvB","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\";\n\nimport { generateAuthToken, verifyPassword, verifyAuthToken, hashPassword } from \"@/utils\"\nimport { ModelAccountFields, ModelUserFields, UserModel } from \"@/models\";\n\nexport const authTypes = {\n operator: \"operator\",\n administrator: \"administrator\",\n worker: \"worker\",\n customer: \"customer\",\n member: \"member\",\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 const AuthService = (props: AuthServiceProps) => {\n\n const {\n secretKey,\n authType = \"administrator\",\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 validateAuthAccount = async (accountCode: string) => {\n\n if (accountCode === \"\") {\n return 0\n }\n\n // check if the accountId & accountCode is not empty\n // if (!accountId && !accountCode) throwError(`accountId or accountCode is required`)\n\n let selectAccountId = 0\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 return Number(selectAccountId ?? 0)\n\n }\n\n const getAuthToken = async ({ authType, data, accountCode = \"\" }: AuthGetTokenProps) => {\n\n let _rest = rest\n let _accountId = 0\n\n if (authType !== \"operator\") {\n _accountId = await validateAuthAccount(accountCode)\n }\n\n // _rest.accountId = accountId\n _rest.accountId = _accountId\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 foundUser: any = {}\n\n // only for operator & administrator\n if (authType === \"operator\" || authType === \"administrator\") {\n\n // const operatorService = UserService({ ...rest })\n\n const _user = await userService.getUserByUsername({\n username: `${data[\"username\"]}`,\n includePassword: true,\n datatypes: getUserInjectionFieldName(authType) as any\n })\n\n foundUser = _user\n\n } else { // other user type\n\n let payload: UserListProps = {\n includePassword: true,\n limit: 1,\n offset: 0,\n sortfield: \"user_id\",\n sortorder: \"DESC\",\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: foundOtherUser } = await userService.listUsers(payload)\n\n foundUser = foundOtherUser.length > 0 ? foundOtherUser[0] : {}\n\n }\n\n if (!foundUser?.user_id) {\n throw Error(`Invalid username: ${data[\"username\"]}`)\n }\n\n let user = foundUser\n // 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),\n // owner_id: 0\n }\n };\n\n return {\n getAuthToken,\n verifyAuthToken,\n }\n\n}\n\n"]}
@@ -38,7 +38,7 @@ const ChangePasswordUtilService = (props) => {
38
38
  if (!user?.user_id) {
39
39
  (0, utils_1.throwError)(`Invalid user id in auth token`);
40
40
  }
41
- if (!account?.account_id && userType !== "operator") { // operator token do not have account id
41
+ if (Object.keys(account).length <= 0) { // must have account id record
42
42
  (0, utils_1.throwError)(`Invalid account id in auth token`);
43
43
  }
44
44
  // new password cannot same with current password
@@ -1 +1 @@
1
- {"version":3,"file":"ChangePasswordUtilService.js","sourceRoot":"","sources":["../../../src/services/utils/ChangePasswordUtilService.ts"],"names":[],"mappings":";;;AAAA,mCAAgF;AAChF,yCAGoB;AAEpB,6CAA6C;AAE7C,mCAAuE;AAahE,MAAM,yBAAyB,GAAG,CAAC,KAAqC,EAAE,EAAE;IAE/E,MAAM,EACF,SAAS,EACT,GAAG,IAAI,EACV,GAAG,KAAK,CAAA;IAET,MAAM,cAAc,GAAG,KAAK,EAAE,EAAE,SAAS,EAAE,eAAe,EAAE,eAAe,EAAE,WAAW,EAAiC,EAAE,EAAE;QAEzH,6BAA6B;QAC7B,MAAM,UAAU,GAAG,IAAA,4BAAoB,GAAE,CAAA;QAEzC,+CAA+C;QAC/C,MAAM,aAAa,GAAG,UAAU,CAAC,aAAa,CAAA;QAE9C,qBAAqB;QACrB,MAAM,OAAO,GAAG,IAAA,uBAAe,EAAC,SAAS,EAAE,aAAa,CAAC,CAAC;QAE1D,MAAM,SAAS,GAAG,OAAO,EAAE,OAAO,EAAE,UAAU,IAAI,EAAE,CAAC;QACrD,MAAM,MAAM,GAAG,OAAO,EAAE,IAAI,EAAE,OAAO,IAAI,EAAE,CAAC;QAC5C,MAAM,QAAQ,GAAG,OAAO,EAAE,IAAI,EAAE,SAAS,IAAI,EAAE,CAAC;QAEhD,IAAI,CAAC,SAAS,IAAI,QAAQ,KAAK,UAAU,EAAE,CAAC,CAAC,wCAAwC;YACjF,IAAA,kBAAU,EAAC,6CAA6C,CAAC,C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CAAC,CAAA;QACtD,CAAC;QAED,6BAA6B;QAC7B,IAAI,CAAC;YACD,MAAM,aAAa,GAAG,MAAM,IAAA,sBAAc,EAAC,eAAe,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;YAC3E,8CAA8C;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,mBAAmB,EAAE,WAAW;aACnC;YACD,UAAU,EAAE,OAAO,CAAC,QAAQ,KAAK,UAAU,CAAC;SAC/C,CAAC,CAAA;QAEF,OAAO,YAAY,CAAA;IAEvB,CAAC,CAAA;IAED,OAAO;QACH,cAAc;KACjB,CAAA;AAEL,CAAC,CAAA;AA/FY,QAAA,yBAAyB,6BA+FrC","sourcesContent":["import { DefaultServiceProps, getCoreConfiguration, throwError } from \"@/utils\";\nimport {\n UserService,\n AccountService,\n} from \"@/services\";\n\n// import { config } from \"@/configs/config\";\n\nimport { verifyPassword, verifyAuthToken, hashPassword } from \"@/utils\"\n\nexport type ChangePasswordUtilServiceProps = DefaultServiceProps & {\n secretKey: string,\n};\n\nexport type ChangePasswordUtilChangeProps = {\n authToken: string,\n currentPassword: string\n newPassword: string\n confirmPassword: string\n};\n\nexport const ChangePasswordUtilService = (props: ChangePasswordUtilServiceProps) => {\n\n const {\n secretKey,\n ...rest\n } = props\n\n const changePassword = async ({ authToken, currentPassword, confirmPassword, newPassword }: ChangePasswordUtilChangeProps) => {\n\n // retrive core configuration\n const coreConfig = getCoreConfiguration()\n\n // const authSecretKey = config.AUTH_SECRET_KEY\n const authSecretKey = coreConfig.authSecretKey\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 unhashLoginPassword: newPassword\n },\n isOperator: Boolean(userType === \"operator\")\n })\n\n return responseUser\n\n }\n\n return {\n changePassword\n }\n\n}\n\n"]}
1
+ {"version":3,"file":"ChangePasswordUtilService.js","sourceRoot":"","sources":["../../../src/services/utils/ChangePasswordUtilService.ts"],"names":[],"mappings":";;;AAAA,mCAAgF;AAChF,yCAGoB;AA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AAG,MAAM,WAAW,CAAC,eAAe,CAAC;YACnD,EAAE,EAAE,MAAM,CAAC,MAAM,CAAC;YAClB,IAAI,EAAE;gBACF,mBAAmB,EAAE,WAAW;aACnC;YACD,UAAU,EAAE,OAAO,CAAC,QAAQ,KAAK,UAAU,CAAC;SAC/C,CAAC,CAAA;QAEF,OAAO,YAAY,CAAA;IAEvB,CAAC,CAAA;IAED,OAAO;QACH,cAAc;KACjB,CAAA;AAEL,CAAC,CAAA;AA/FY,QAAA,yBAAyB,6BA+FrC","sourcesContent":["import { DefaultServiceProps, getCoreConfiguration, throwError } from \"@/utils\";\nimport {\n UserService,\n AccountService,\n} from \"@/services\";\n\n// import { config } from \"@/configs/config\";\n\nimport { verifyPassword, verifyAuthToken, hashPassword } from \"@/utils\"\n\nexport type ChangePasswordUtilServiceProps = DefaultServiceProps & {\n secretKey: string,\n};\n\nexport type ChangePasswordUtilChangeProps = {\n authToken: string,\n currentPassword: string\n newPassword: string\n confirmPassword: string\n};\n\nexport const ChangePasswordUtilService = (props: ChangePasswordUtilServiceProps) => {\n\n const {\n secretKey,\n ...rest\n } = props\n\n const changePassword = async ({ authToken, currentPassword, confirmPassword, newPassword }: ChangePasswordUtilChangeProps) => {\n\n // retrive core configuration\n const coreConfig = getCoreConfiguration()\n\n // const authSecretKey = config.AUTH_SECRET_KEY\n const authSecretKey = coreConfig.authSecretKey\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 (Object.keys(account).length <= 0) { // must have account id record\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 unhashLoginPassword: newPassword\n },\n isOperator: Boolean(userType === \"operator\")\n })\n\n return responseUser\n\n }\n\n return {\n changePassword\n }\n\n}\n\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storemw-core-api",
3
- "version": "1.0.138",
3
+ "version": "1.0.139",
4
4
  "description": "STOREMW Core API",
5
5
  "main": "dist/app.js",
6
6
  "types": "dist/app.d.ts",