From 6b2a34151190d376a06dd12ef67f500a7a1daaac Mon Sep 17 00:00:00 2001 From: ghost2023 Date: Thu, 28 May 2026 10:32:46 +0300 Subject: [PATCH] refactor(services): Consolidate user authentication logic and add customer API --- .../portal/src/services/account.ts | 92 ------------------ .../portal/src/services/api.ts | 95 +++++++++++++++++++ .../portal/src/services/auth.service.ts | 78 +++++++++++++++ apps/edr-freight-web/portal/src/types/auth.ts | 65 +++++++++++++ .../portal/src/types/createUser.ts | 10 -- .../src/types/generateVerificationCode.ts | 5 - 6 files changed, 238 insertions(+), 107 deletions(-) delete mode 100644 apps/edr-freight-web/portal/src/services/account.ts create mode 100644 apps/edr-freight-web/portal/src/services/auth.service.ts create mode 100644 apps/edr-freight-web/portal/src/types/auth.ts delete mode 100644 apps/edr-freight-web/portal/src/types/createUser.ts delete mode 100644 apps/edr-freight-web/portal/src/types/generateVerificationCode.ts diff --git a/apps/edr-freight-web/portal/src/services/account.ts b/apps/edr-freight-web/portal/src/services/account.ts deleted file mode 100644 index 6e97e2dd0..000000000 --- a/apps/edr-freight-web/portal/src/services/account.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { URL_CONSTANTS } from "@/constants/URLS"; -import { CreateUserPayload } from "@/types/createUser"; -import { VerificationCodePayload } from "@/types/generateVerificationCode"; -import { UserTypeRequest } from "@/types/userTypeRequest"; -import { client } from "@/utils/api"; -import { ApiResponse } from "@edr/types"; -import { GenerateVerifcationCodePayload } from "node_modules/@tria-plc/iamui-common/dist/types/shared/services/authService"; - -// ----------------------------------------------------------------------------- -// API -// ----------------------------------------------------------------------------- - -export const createUser = async ( - body: CreateUserPayload -) => { - const res = - await client.post< - ApiResponse - >( - URL_CONSTANTS.USERS.SIGN_UP, - body - ); - - return res.data; -}; - -export const getMyInfo = async () => { - const res = - await client.get< - ApiResponse - >( - URL_CONSTANTS.USERS.ME - ); - - return res.data; -}; - -export const generateVerificationCode = async ( - body: VerificationCodePayload -) => { - const res = - await client.patch< - ApiResponse - >( - URL_CONSTANTS.USERS.GENERATE_VERIFICATION_CODE, - body - ); - - return res.data.data; -}; - -export const setPassword = async ( - body: any -) => { - const res = - await client.patch< - ApiResponse - >( - URL_CONSTANTS.USERS.SET_PASSWORD, - body - ); - - return res.data.data; -}; - -export const createOTP = async ( - body: any -) => { - const res = - await client.post< - ApiResponse - >( - URL_CONSTANTS.OTP.SEND, - body - ); - - return res.data; -}; - -export const verifyOTP = async ( - body: any -) => { - const res = - await client.post< - ApiResponse - >( - URL_CONSTANTS.OTP.VERIFY, - body - ); - - return res.data; -}; \ No newline at end of file diff --git a/apps/edr-freight-web/portal/src/services/api.ts b/apps/edr-freight-web/portal/src/services/api.ts index ba2b227c6..d05807090 100644 --- a/apps/edr-freight-web/portal/src/services/api.ts +++ b/apps/edr-freight-web/portal/src/services/api.ts @@ -13,6 +13,8 @@ import { consignmentsService } from "./consignments.service"; import { trackingService } from "./tracking.service"; import { fileUploadSettingsService } from "./fileUploadSettings.service"; import { dropdownSettingsService } from "./dropdownSettings.service"; +import { authService } from "./auth.service"; +import { customersService } from "./customers.service"; import { CreateDropdownOptionDto, CreateDropdownSettingDto, @@ -21,12 +23,105 @@ import { UpdateDropdownOptionDto, UpdateDropdownSettingDto, } from "@/types/dropdownSettings"; +import { + CreateCustomerDto, + Customer, + UpdateCustomerDto, +} from "@/types/customers"; +import type { + AuthUser, + GenerateVerificationCodePayload, + LoginPayload, + LoginResponse, + OtpPayload, + OtpResponse, + SetPasswordPayload, + SignupPayload, + SignupResponse, +} from "@/types/auth"; // --------------------------------------------------------------------------- // API definition // --------------------------------------------------------------------------- export const api = { + auth: { + login: endpoint( + "auth", + "login", + authService.login, + ), + createUser: endpoint( + "auth", + "createUser", + authService.createUser, + ), + getMyInfo: endpoint( + "auth", + "getMyInfo", + authService.getMyInfo, + ), + generateVerificationCode: endpoint( + "auth", + "generateVerificationCode", + authService.generateVerificationCode, + ), + setPassword: endpoint( + "auth", + "setPassword", + authService.setPassword, + ), + sendOTP: endpoint( + "auth", + "sendOTP", + authService.sendOTP, + ), + verifyOTP: endpoint( + "auth", + "verifyOTP", + authService.verifyOTP, + ), + logout: endpoint( + "auth", + "logout", + authService.logout, + ), + }, + + customers: { + list: endpoint( + "customers", + "list", + customersService.list, + ), + + get: endpoint<{ id: string }, Customer>("customers", "get", ({ id }) => + customersService.getById(id), + ), + + create: endpoint( + "customers", + "create", + customersService.create, + ), + + update: endpoint<{ id: string; dto: UpdateCustomerDto }, Customer>( + "customers", + "update", + ({ id, dto }) => customersService.update(id, dto), + ), + + remove: endpoint<{ id: string }, void>("customers", "remove", ({ id }) => + customersService.remove(id), + ), + + getByUserId: endpoint<{ id: string }, Customer>( + "customers", + "getByUserId", + ({ id }) => customersService.getByUserId(id), + ), + }, + bookings: { list: endpoint>( "bookings", diff --git a/apps/edr-freight-web/portal/src/services/auth.service.ts b/apps/edr-freight-web/portal/src/services/auth.service.ts new file mode 100644 index 000000000..bb162fbea --- /dev/null +++ b/apps/edr-freight-web/portal/src/services/auth.service.ts @@ -0,0 +1,78 @@ +import { URL_CONSTANTS } from "@/constants/URLS"; +import { client } from "@/utils/api"; +import { ApiResponse } from "@edr/types"; +import type { + AuthUser, + GenerateVerificationCodePayload, + LoginPayload, + LoginResponse, + OtpPayload, + OtpResponse, + SetPasswordPayload, + SignupPayload, + SignupResponse, +} from "@/types/auth"; + +export const authService = { + login: async (body: LoginPayload) => { + const res = await client.post>( + URL_CONSTANTS.AUTH.LOGIN, + body, + ); + return res.data.data; + }, + + createUser: async (body: SignupPayload) => { + const res = await client.post>( + URL_CONSTANTS.USERS.SIGN_UP, + body, + ); + return res.data.data; + }, + + getMyInfo: async () => { + const res = await client.get>( + URL_CONSTANTS.USERS.ME, + ); + return res.data.data; + }, + + generateVerificationCode: async (body: GenerateVerificationCodePayload) => { + const res = await client.patch>( + URL_CONSTANTS.USERS.GENERATE_VERIFICATION_CODE, + body, + ); + return res.data.data; + }, + + setPassword: async (body: SetPasswordPayload) => { + const res = await client.patch>( + URL_CONSTANTS.USERS.SET_PASSWORD, + body, + ); + return res.data.data; + }, + + sendOTP: async (body: OtpPayload) => { + const res = await client.post>( + URL_CONSTANTS.OTP.SEND, + body, + ); + return res.data.data; + }, + + verifyOTP: async (body: OtpPayload) => { + const res = await client.post>( + URL_CONSTANTS.OTP.VERIFY, + body, + ); + return res.data.data; + }, + + logout: async () => { + const res = await client.patch>( + URL_CONSTANTS.AUTH.LOGOUT, + ); + return res.data.data; + }, +}; diff --git a/apps/edr-freight-web/portal/src/types/auth.ts b/apps/edr-freight-web/portal/src/types/auth.ts new file mode 100644 index 000000000..08318417d --- /dev/null +++ b/apps/edr-freight-web/portal/src/types/auth.ts @@ -0,0 +1,65 @@ +export interface AuthUser { + id: string; + name: { am: string; en: string }; + email: string; + roles: string[]; + status: string; + employee: any[]; + userType: string; + username: string; + permissions: string[]; + phoneNumber: string; + sharepointId: string | null; + hasSetPassword: boolean; + hasFinishedRegistration: boolean; + hasFinishedDMSOnboarding: boolean; +} + +export interface SignupPayload { + email: string; + username: string; + phoneNumber: string; + userType: string; + name: { en: string; am: string }; +} + +export interface SignupResponse { + token: string; + refreshToken: string; + otp: string; + userId: string; +} + +export interface OtpPayload { + phone: string; + otp: string; +} + +export interface OtpResponse { + success: boolean; + message: string; +} + +export interface SetPasswordPayload { + newPassword: string; + confirmPassword: string; + userId: string; + email: string; + verificationCode: string; +} + +export interface GenerateVerificationCodePayload { + email: string; + phoneNumber: string; + type: string; +} + +export interface LoginPayload { + email: string; + password: string; +} + +export interface LoginResponse { + token: string; + refreshToken: string; +} diff --git a/apps/edr-freight-web/portal/src/types/createUser.ts b/apps/edr-freight-web/portal/src/types/createUser.ts deleted file mode 100644 index 53bd6c8e9..000000000 --- a/apps/edr-freight-web/portal/src/types/createUser.ts +++ /dev/null @@ -1,10 +0,0 @@ -export type CreateUserPayload = { - email: string; - username: string; - phoneNumber: string; - userType: string; - name: { - en: string; - am?: string; - }; -}; \ No newline at end of file diff --git a/apps/edr-freight-web/portal/src/types/generateVerificationCode.ts b/apps/edr-freight-web/portal/src/types/generateVerificationCode.ts deleted file mode 100644 index 24f0e206d..000000000 --- a/apps/edr-freight-web/portal/src/types/generateVerificationCode.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type VerificationCodePayload = { - email: string; - phoneNumber: string; - type: string; -}; \ No newline at end of file