Nhảy đến nội dung

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

✅ 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:

  • Chúng tôi không xử lý lỗi trong các ví dụ này.
  • Bạn có thể xử lý lỗi trong trycatch hoặc trong một cấu trúc như "await Promise.catch((error) => error)"

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 marker 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": 83,
"storageId": 1,
"createdDate": "2025-04-27T22:07:50.620Z",
"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": 84,
"storageId": 1,
"createdDate": "2025-04-27T22:08:22.813Z",
"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": 2
}

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 marker lưu trữ đơn hàng với Orders.getAllOrdersByMarker()
const allOrders = await Orders.getAllOrdersByMarker('orders', 'en_US', 0, 30);
console.log(allOrders);