Skip to content

Quản lý Khách hàng v1.0.0

Mã nguồn: src/services/customer.service.ts · src/controllers/customer/customer.controller.ts

Thông tin chính

Thuộc tínhGiá trị
Route/customers
Phạm viChỉ Organizer (KHÔNG giới hạn theo merchant)
Role tự động010_customer (priority 10)
Trạng thái tự độngACTIVATED
AuthJWT + Basic
Mapping1 PolicyDefinition: USER→ORGANIZER

Customer là User với role cố định CUSTOMER. Không có username, không có password, không có mapping với merchant.

Mô hình dữ liệu

Bản ghi PolicyDefinition cho mỗi Customer

#variantsubjectType → targetTypetargetIdTạo bởi
1groupUserRolerole ID 010_customerUserService.create()
2groupUserOrganizerorganizer IDCustomerService._createUserPolicies()

Luồng tạo mới

Luồng cập nhật

Xóa

Soft-delete thông qua UserRepository.deleteById() — gán giá trị timestamp cho deletedAt.

Schema Request / Response

CreateCustomerRequest

Kế thừa từ CreateUserRequest với một số trường được loại bỏ:

TrườngKiểuBắt buộcGhi chú
emailsstring[]tối thiểu 1
phonesstring[]tối thiểu 1, định dạng E.164
profileobjectfirstName, lastName, birthday?, locale?
organizerIdstringLiên kết customer với organizer

roleIds, username, credential, status được loại bỏ — CustomerService tự xử lý.

UpdateByIdCustomerRequest

TrườngKiểuGhi chú
emailsstring[]?tối thiểu 1 nếu cung cấp
phonesstring[]?tối thiểu 1 nếu cung cấp
profileobject?cập nhật một phần

roleIdsstatus được loại bỏ — customer luôn giữ role CUSTOMER.

CreateCustomerResponse

typescript
{
  id: string,
  status: 'ACTIVATED',
  isActive: boolean,
  createdAt: string,
  modifiedAt: string,
  profile?: {
    firstName, lastName, emails[], phones[],
    birthday?, locale, metadata?
  }
}

Phân quyền tại Controller

Tất cả thao tác với customer được giới hạn phạm vi qua UserRepository.findIdsByPoliciesForCurrentUser() với roleIdentifiers: ['010_customer'].

Thao tácPhạm vi
find / count / findOneLọc theo các ID đã giới hạn theo policy + tham số organizerId tùy chọn
findById / deleteByIdKiểm tra ID có nằm trong tập đã giới hạn theo policy
createXác thực quyền sở hữu organizer thông qua PolicyDefinition
updateByIdXác thực việc cùng thuộc một organizer thông qua PolicyDefinition
deleteByÁp dụng bộ lọc id IN scoped IDs

API Endpoints

MethodPathMô tả
GET/customersDanh sách (giới hạn theo organizer, phân trang)
GET/customers/countĐếm số lượng (giới hạn theo organizer)
GET/customers/find-oneTìm một bản ghi (giới hạn theo organizer)
GET/customers/:idLấy theo ID
POST/customersTạo customer
PUT/customers/:idCập nhật customer
DELETE/customers/:idSoft-delete
DELETE/customersXóa hàng loạt (giới hạn theo organizer)

Ví dụ Request

Tạo mới

http
POST /v1/api/identity/customers
Authorization: Bearer <token>

{
  "emails": ["customer@example.com"],
  "phones": ["+84901234567"],
  "profile": {
    "firstName": "Nguyen",
    "lastName": "Van A",
    "birthday": "1990-05-15",
    "locale": "vi-VN"
  },
  "organizerId": "org-123"
}

Cập nhật

http
PUT /v1/api/identity/customers/cust-456
Authorization: Bearer <token>

{
  "profile": { "lastName": "Van B" },
  "phones": ["+84907654321"]
}

Phụ thuộc

Quyết định Thiết kế

Quyết địnhLý do
Customer = User + roleTái sử dụng hạ tầng identity; mô hình auth nhất quán
Chỉ giới hạn theo organizerCustomer thuộc về organizer, không thuộc về từng merchant riêng lẻ
Không có thông tin đăng nhậpusername + credential được loại bỏ khỏi request
Soft deleteBảo toàn lịch sử giao dịch thông qua deletedAt

Trang liên quan

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.