refactor(services): Consolidate user authentication logic and add customer API

This commit is contained in:
ghost2023
2026-05-28 10:32:46 +03:00
parent 67d9d13591
commit 6b2a341511
6 changed files with 238 additions and 107 deletions

View File

@@ -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<any>
>(
URL_CONSTANTS.USERS.SIGN_UP,
body
);
return res.data;
};
export const getMyInfo = async () => {
const res =
await client.get<
ApiResponse<any>
>(
URL_CONSTANTS.USERS.ME
);
return res.data;
};
export const generateVerificationCode = async (
body: VerificationCodePayload
) => {
const res =
await client.patch<
ApiResponse<string>
>(
URL_CONSTANTS.USERS.GENERATE_VERIFICATION_CODE,
body
);
return res.data.data;
};
export const setPassword = async (
body: any
) => {
const res =
await client.patch<
ApiResponse<string>
>(
URL_CONSTANTS.USERS.SET_PASSWORD,
body
);
return res.data.data;
};
export const createOTP = async (
body: any
) => {
const res =
await client.post<
ApiResponse<any>
>(
URL_CONSTANTS.OTP.SEND,
body
);
return res.data;
};
export const verifyOTP = async (
body: any
) => {
const res =
await client.post<
ApiResponse<any>
>(
URL_CONSTANTS.OTP.VERIFY,
body
);
return res.data;
};

View File

@@ -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<LoginPayload, LoginResponse>(
"auth",
"login",
authService.login,
),
createUser: endpoint<SignupPayload, SignupResponse>(
"auth",
"createUser",
authService.createUser,
),
getMyInfo: endpoint<void, AuthUser>(
"auth",
"getMyInfo",
authService.getMyInfo,
),
generateVerificationCode: endpoint<GenerateVerificationCodePayload, string>(
"auth",
"generateVerificationCode",
authService.generateVerificationCode,
),
setPassword: endpoint<SetPasswordPayload, void>(
"auth",
"setPassword",
authService.setPassword,
),
sendOTP: endpoint<OtpPayload, OtpResponse>(
"auth",
"sendOTP",
authService.sendOTP,
),
verifyOTP: endpoint<OtpPayload, OtpResponse>(
"auth",
"verifyOTP",
authService.verifyOTP,
),
logout: endpoint<void, void>(
"auth",
"logout",
authService.logout,
),
},
customers: {
list: endpoint<void, Customer[]>(
"customers",
"list",
customersService.list,
),
get: endpoint<{ id: string }, Customer>("customers", "get", ({ id }) =>
customersService.getById(id),
),
create: endpoint<CreateCustomerDto, Customer>(
"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<void, PaginatedResponse<Freight.IBooking>>(
"bookings",

View File

@@ -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<ApiResponse<LoginResponse>>(
URL_CONSTANTS.AUTH.LOGIN,
body,
);
return res.data.data;
},
createUser: async (body: SignupPayload) => {
const res = await client.post<ApiResponse<SignupResponse>>(
URL_CONSTANTS.USERS.SIGN_UP,
body,
);
return res.data.data;
},
getMyInfo: async () => {
const res = await client.get<ApiResponse<AuthUser>>(
URL_CONSTANTS.USERS.ME,
);
return res.data.data;
},
generateVerificationCode: async (body: GenerateVerificationCodePayload) => {
const res = await client.patch<ApiResponse<string>>(
URL_CONSTANTS.USERS.GENERATE_VERIFICATION_CODE,
body,
);
return res.data.data;
},
setPassword: async (body: SetPasswordPayload) => {
const res = await client.patch<ApiResponse<void>>(
URL_CONSTANTS.USERS.SET_PASSWORD,
body,
);
return res.data.data;
},
sendOTP: async (body: OtpPayload) => {
const res = await client.post<ApiResponse<OtpResponse>>(
URL_CONSTANTS.OTP.SEND,
body,
);
return res.data.data;
},
verifyOTP: async (body: OtpPayload) => {
const res = await client.post<ApiResponse<OtpResponse>>(
URL_CONSTANTS.OTP.VERIFY,
body,
);
return res.data.data;
},
logout: async () => {
const res = await client.patch<ApiResponse<void>>(
URL_CONSTANTS.AUTH.LOGOUT,
);
return res.data.data;
},
};

View File

@@ -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;
}

View File

@@ -1,10 +0,0 @@
export type CreateUserPayload = {
email: string;
username: string;
phoneNumber: string;
userType: string;
name: {
en: string;
am?: string;
};
};

View File

@@ -1,5 +0,0 @@
export type VerificationCodePayload = {
email: string;
phoneNumber: string;
type: string;
};