Nhảy đến nội dung

Lịch sử đơn hàng của người dùng

Trong ví dụ này, chúng tôi sẽ trình bày cách lấy và hiển thị lịch sử đơn hàng của người dùng bằng cách sử dụng API OneEntry.

✅ Mục đích của kịch bản:

  • Người dùng đăng nhập bằng thông tin xác thực của họ
  • Chúng tôi lấy lịch sử đơn hàng của người dùng

✅ Những gì bạn cần:

  • Một PROJECT_URL và APP_TOKEN hợp lệ để xác thực với API OneEntry.
  • Người dùng đã đăng ký với các đơn hàng

📌 Quan trọng:

  • Những ví dụ này không bao gồm xử lý lỗi.
  • Bạn có thể quản lý lỗi bằng cách sử dụng khối try-catch hoặc bằng cách sử dụng một cấu trúc như await Promise.catch((error) => error).
  • Đảm bảo rằng dấu hiệu lưu trữ đơn hàng được sử dụng trong Orders.getAllOrdersByMarker() khớp với cái được cấu hình trong dự án OneEntry của bạn.

Kịch bản

1. Nhập oneEntry và định nghĩa url và token

Ví dụ:

import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

2. Tạo một client API với hàm defineOneEntry()

Ví dụ:

const { AuthProvider, Orders } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. xác thực với AuthProvider.auth()

Dữ liệu:

[
{
"marker": "email_reg",
"value": "your-user@email.com"
},
{
"marker": "password_reg",
"value": "123456"
}
]

Ví dụ:

const authResponse = await AuthProvider.auth('email', {
authData,
});
Kết quả:
{
"userIdentifier": "your-user@email.com",
"authProviderIdentifier": "email",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR...pZCI6MTYsImF1dGhQ"
"refreshToken": "1745494429101-...-2834edf8"
}

4. Lấy tất cả đơn hàng của người dùng theo dấu hiệu lưu trữ đơn hàng với Orders.getAllOrdersByMarker()

Ví dụ:

const allOrders = await Orders.getAllOrdersByMarker('orders', 'en_US', 0, 30);
Kết quả:
{
"items": [
{
"id": 98,
"storageId": 1,
"createdDate": "2025-10-23T13:42:45.421Z",
"statusIdentifier": "upcoming",
"formIdentifier": "order",
"formData": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
],
"attributeSetIdentifier": "order",
"totalSum": "340.00",
"currency": "USD",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 14,
"title": "Bóng xanh",
"sku": null,
"previewImage": [],
"price": 340,
"quantity": 1
}
],
"isCompleted": false
},
{
"id": 97,
"storageId": 1,
"createdDate": "2025-08-29T17:33:30.141Z",
"statusIdentifier": "upcoming",
"formIdentifier": "order",
"formData": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
],
"attributeSetIdentifier": "order",
"totalSum": "340.00",
"currency": "USD",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 14,
"title": "Bóng xanh",
"sku": null,
"previewImage": [],
"price": 340,
"quantity": 1
}
],
"isCompleted": false
},
{
"id": 96,
"storageId": 1,
"createdDate": "2025-08-29T17:32:53.278Z",
"statusIdentifier": "upcoming",
"formIdentifier": "order",
"formData": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
],
"attributeSetIdentifier": "order",
"totalSum": "340.00",
"currency": "USD",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 14,
"title": "Bóng xanh",
"sku": null,
"previewImage": [],
"price": 340,
"quantity": 1
}
],
"isCompleted": false
},
{
"id": 93,
"storageId": 1,
"createdDate": "2025-08-28T21:39:04.945Z",
"statusIdentifier": "upcoming",
"formIdentifier": "order",
"formData": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
],
"attributeSetIdentifier": "order",
"totalSum": "340.00",
"currency": "USD",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 14,
"title": "Bóng xanh",
"sku": null,
"previewImage": [],
"price": 340,
"quantity": 1
}
],
"isCompleted": false
},
{
"id": 89,
"storageId": 1,
"createdDate": "2025-04-30T20:15:29.254Z",
"statusIdentifier": "upcoming",
"formIdentifier": "order",
"formData": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
],
"attributeSetIdentifier": "order",
"totalSum": "340.00",
"currency": "USD",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 14,
"title": "Bóng xanh",
"sku": null,
"previewImage": [],
"price": 340,
"quantity": 1
}
],
"isCompleted": false
},
{
"id": 88,
"storageId": 1,
"createdDate": "2025-04-30T20:15:12.616Z",
"statusIdentifier": "upcoming",
"formIdentifier": "order",
"formData": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
],
"attributeSetIdentifier": "order",
"totalSum": "340.00",
"currency": "USD",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 14,
"title": "Bóng xanh",
"sku": null,
"previewImage": [],
"price": 340,
"quantity": 1
}
],
"isCompleted": false
}
],
"total": 6
}

Ví dụ cuối cùng

// 1. Nhập oneEntry và định nghĩa PROJECT_URL và APP_TOKEN
import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

// 2. Tạo một client API
const { AuthProvider, Orders } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. xác thực với AuthProvider.auth()
const authData = [
{
marker: 'email_reg',
value: 'your-user@email.com',
},
{
marker: 'password_reg',
value: '123456',
},
];
const authResponse = await AuthProvider.auth('email', {
authData,
});

// 4. Lấy tất cả đơn hàng của người dùng theo dấu hiệu lưu trữ đơn hàng với Orders.getAllOrdersByMarker()
const allOrders = await Orders.getAllOrdersByMarker('orders', 'en_US', 0, 30);
console.log(allOrders);