@comity/order 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/cjs/contracts/address-snapshot.js +3 -0
- package/dist/cjs/contracts/contact.js +3 -0
- package/dist/cjs/contracts/customer-snapshot.js +3 -0
- package/dist/cjs/contracts/item.js +3 -0
- package/dist/cjs/contracts/order-repository.js +3 -0
- package/dist/cjs/contracts/order.js +3 -0
- package/dist/cjs/contracts/payment-snapshot.js +3 -0
- package/dist/cjs/domain/order-transitions.js +52 -0
- package/dist/cjs/entities/order.js +459 -0
- package/dist/cjs/errors/index.js +6 -0
- package/dist/cjs/errors/order.js +39 -0
- package/dist/cjs/index.js +8 -0
- package/dist/cjs/repositories/index.js +6 -0
- package/dist/cjs/repositories/memory.js +69 -0
- package/dist/cjs/value-objects/order-id.js +70 -0
- package/dist/esm/contracts/address-snapshot.js +2 -0
- package/dist/esm/contracts/contact.js +2 -0
- package/dist/esm/contracts/customer-snapshot.js +2 -0
- package/dist/esm/contracts/item.js +2 -0
- package/dist/esm/contracts/order-repository.js +2 -0
- package/dist/esm/contracts/order.js +2 -0
- package/dist/esm/contracts/payment-snapshot.js +2 -0
- package/dist/esm/domain/order-transitions.js +48 -0
- package/dist/esm/entities/order.js +455 -0
- package/dist/esm/errors/index.js +2 -0
- package/dist/esm/errors/order.js +35 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/repositories/index.js +2 -0
- package/dist/esm/repositories/memory.js +65 -0
- package/dist/esm/value-objects/order-id.js +66 -0
- package/dist/types/contracts/address-snapshot.d.ts +35 -0
- package/dist/types/contracts/contact.d.ts +14 -0
- package/dist/types/contracts/customer-snapshot.d.ts +24 -0
- package/dist/types/contracts/item.d.ts +100 -0
- package/dist/types/contracts/order-repository.d.ts +82 -0
- package/dist/types/contracts/order.d.ts +108 -0
- package/dist/types/contracts/payment-snapshot.d.ts +36 -0
- package/dist/types/domain/order-transitions.d.ts +22 -0
- package/dist/types/entities/order.d.ts +168 -0
- package/dist/types/errors/index.d.ts +2 -0
- package/dist/types/errors/order.d.ts +44 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/repositories/index.d.ts +1 -0
- package/dist/types/repositories/memory.d.ts +26 -0
- package/dist/types/value-objects/order-id.d.ts +50 -0
- package/package.json +102 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderId = exports.Order = void 0;
|
|
4
|
+
var order_js_1 = require("./entities/order.js");
|
|
5
|
+
Object.defineProperty(exports, "Order", { enumerable: true, get: function () { return order_js_1.Order; } });
|
|
6
|
+
var order_id_js_1 = require("./value-objects/order-id.js");
|
|
7
|
+
Object.defineProperty(exports, "OrderId", { enumerable: true, get: function () { return order_id_js_1.OrderId; } });
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryOrderRepository = void 0;
|
|
4
|
+
var memory_js_1 = require("./memory.js");
|
|
5
|
+
Object.defineProperty(exports, "MemoryOrderRepository", { enumerable: true, get: function () { return memory_js_1.MemoryOrderRepository; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryOrderRepository = void 0;
|
|
4
|
+
const result_1 = require("@comity/primitives/result");
|
|
5
|
+
/**
|
|
6
|
+
* In-memory implementation of `OrderRepository` for testing and development purposes.
|
|
7
|
+
*
|
|
8
|
+
* Note: This implementation is not suitable for production use as it does not persist
|
|
9
|
+
* sessions and is not shared across multiple instances of the application.
|
|
10
|
+
*/
|
|
11
|
+
class MemoryOrderRepository {
|
|
12
|
+
/** Internal storage keyed by tenant + order ID */
|
|
13
|
+
#orders = new Map();
|
|
14
|
+
#makeKey(tenant, id) {
|
|
15
|
+
return `${tenant.toString()}\u0000${id.toString()}`;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* @inheritdoc
|
|
19
|
+
*/
|
|
20
|
+
async getById(id, ctx) {
|
|
21
|
+
const order = this.#orders.get(this.#makeKey(ctx.tenant, id));
|
|
22
|
+
if (!order) {
|
|
23
|
+
return (0, result_1.success)(null);
|
|
24
|
+
}
|
|
25
|
+
return (0, result_1.success)(order);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @inheritdoc
|
|
29
|
+
*/
|
|
30
|
+
async save(order, ctx) {
|
|
31
|
+
this.#orders.set(this.#makeKey(ctx.tenant, order.id), order);
|
|
32
|
+
return (0, result_1.success)(undefined);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @inheritdoc
|
|
36
|
+
*/
|
|
37
|
+
async search(criteria, ctx) {
|
|
38
|
+
const tenantPrefix = `${ctx.tenant.toString()}\u0000`;
|
|
39
|
+
const all = [...this.#orders.entries()]
|
|
40
|
+
.filter(([key]) => key.startsWith(tenantPrefix))
|
|
41
|
+
.map(([, order]) => order);
|
|
42
|
+
let filtered = all;
|
|
43
|
+
if (criteria?.status !== undefined) {
|
|
44
|
+
filtered = filtered.filter((o) => o.status === criteria.status);
|
|
45
|
+
}
|
|
46
|
+
const limit = criteria?.limit ?? filtered.length;
|
|
47
|
+
const offset = criteria?.offset ?? 0;
|
|
48
|
+
const items = filtered.slice(offset, offset + limit).map((o) => ({
|
|
49
|
+
id: o.id,
|
|
50
|
+
status: o.status,
|
|
51
|
+
createdAt: o.createdAt,
|
|
52
|
+
updatedAt: o.updatedAt,
|
|
53
|
+
items: o.items,
|
|
54
|
+
price: o.price,
|
|
55
|
+
channelId: o.channelId,
|
|
56
|
+
...(o.customer !== undefined ? { customer: o.customer } : {}),
|
|
57
|
+
...(o.addresses !== undefined ? { addresses: o.addresses } : {}),
|
|
58
|
+
...(o.payments !== undefined ? { payments: o.payments } : {}),
|
|
59
|
+
...(o.meta !== undefined ? { meta: o.meta } : {}),
|
|
60
|
+
}));
|
|
61
|
+
const total = filtered.length;
|
|
62
|
+
return (0, result_1.success)({
|
|
63
|
+
items,
|
|
64
|
+
total,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.MemoryOrderRepository = MemoryOrderRepository;
|
|
69
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderId = void 0;
|
|
4
|
+
const result_1 = require("@comity/primitives/result");
|
|
5
|
+
const errors_1 = require("@comity/primitives/errors");
|
|
6
|
+
/**
|
|
7
|
+
* OrderId is a value object that represents the unique identifier of an order.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* OrderId is globally unique. The current implementation generates UUIDs.
|
|
11
|
+
* The repository uses a composite key of `TenantId + OrderId` as its physical
|
|
12
|
+
* storage namespace, but the OrderId itself is globally unique and not scoped
|
|
13
|
+
* to a tenant.
|
|
14
|
+
*/
|
|
15
|
+
class OrderId {
|
|
16
|
+
#value;
|
|
17
|
+
/**
|
|
18
|
+
* Creates an OrderId from an identifier string.
|
|
19
|
+
*
|
|
20
|
+
* The Value Object is always created in a valid state; an empty or
|
|
21
|
+
* whitespace-only value yields an `empty` failure instead of throwing.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The value of the order ID.
|
|
24
|
+
*
|
|
25
|
+
* @returns The OrderId, or an `empty` error when the value is empty or
|
|
26
|
+
* whitespace-only.
|
|
27
|
+
*/
|
|
28
|
+
static create(value) {
|
|
29
|
+
if (value.trim().length === 0) {
|
|
30
|
+
return (0, result_1.failure)(new errors_1.InvalidIdentifierError("empty", {
|
|
31
|
+
details: { kind: "OrderId" },
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
return (0, result_1.success)(new OrderId(value));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @param value - The value of the order ID.
|
|
38
|
+
*/
|
|
39
|
+
constructor(value) {
|
|
40
|
+
this.#value = value;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns the underlying string value of the order ID.
|
|
44
|
+
*
|
|
45
|
+
* @returns The underlying identifier.
|
|
46
|
+
*/
|
|
47
|
+
get value() {
|
|
48
|
+
return this.#value;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Checks if this OrderId is equal to another OrderId.
|
|
52
|
+
*
|
|
53
|
+
* @param other - The other OrderId to compare with.
|
|
54
|
+
*
|
|
55
|
+
* @returns True if the OrderIds are equal, false otherwise.
|
|
56
|
+
*/
|
|
57
|
+
equals(other) {
|
|
58
|
+
return this.#value === other.toString();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns a string representation of the order ID.
|
|
62
|
+
*
|
|
63
|
+
* @returns The string representation of the order ID.
|
|
64
|
+
*/
|
|
65
|
+
toString() {
|
|
66
|
+
return this.#value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.OrderId = OrderId;
|
|
70
|
+
//# sourceMappingURL=order-id.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { failure, success } from "@comity/primitives/result";
|
|
2
|
+
import { OrderError } from "../errors/order.js";
|
|
3
|
+
/**
|
|
4
|
+
* Legal transitions for each order status.
|
|
5
|
+
*
|
|
6
|
+
* - `draft` → `pending` (submit)
|
|
7
|
+
* - `pending` → `confirmed` (confirm)
|
|
8
|
+
* - `confirmed` → `fulfilled` (fulfill)
|
|
9
|
+
* - `draft | pending | confirmed` → `cancelled` (cancel)
|
|
10
|
+
*
|
|
11
|
+
* `fulfilled` and `cancelled` are terminal: no transition is legal from them.
|
|
12
|
+
*/
|
|
13
|
+
const TRANSITIONS = {
|
|
14
|
+
draft: ["pending", "cancelled"],
|
|
15
|
+
pending: ["confirmed", "cancelled"],
|
|
16
|
+
confirmed: ["fulfilled", "cancelled"],
|
|
17
|
+
fulfilled: [],
|
|
18
|
+
cancelled: [],
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Checks whether a status transition is legal.
|
|
22
|
+
*
|
|
23
|
+
* @param from - Source status.
|
|
24
|
+
* @param to - Target status.
|
|
25
|
+
*
|
|
26
|
+
* @returns True if the transition is allowed, false otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export function canTransition(from, to) {
|
|
29
|
+
return TRANSITIONS[from].includes(to);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Applies a status transition.
|
|
33
|
+
*
|
|
34
|
+
* @param from - Source status.
|
|
35
|
+
* @param to - Target status.
|
|
36
|
+
*
|
|
37
|
+
* @returns The target status, or an `invalid_status_transition` error when the
|
|
38
|
+
* transition is not allowed.
|
|
39
|
+
*/
|
|
40
|
+
export function transitionOrderStatus(from, to) {
|
|
41
|
+
if (!canTransition(from, to)) {
|
|
42
|
+
return failure(new OrderError("invalid_status_transition", {
|
|
43
|
+
details: { from, to },
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
return success(to);
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=order-transitions.js.map
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { failure, success } from "@comity/primitives/result";
|
|
2
|
+
import { Instant } from "@comity/primitives/time";
|
|
3
|
+
import { transitionOrderStatus } from "../domain/order-transitions.js";
|
|
4
|
+
import { OrderError } from "../errors/order.js";
|
|
5
|
+
/**
|
|
6
|
+
* Returns a defensive copy of a product snapshot so later mutations of the
|
|
7
|
+
* input cannot leak into the order.
|
|
8
|
+
*
|
|
9
|
+
* @param snapshot - The product snapshot to copy.
|
|
10
|
+
*
|
|
11
|
+
* @returns A defensive copy of the snapshot.
|
|
12
|
+
*/
|
|
13
|
+
function copyProductSnapshot(snapshot) {
|
|
14
|
+
/**
|
|
15
|
+
* Returns a defensive copy of a variant snapshot.
|
|
16
|
+
*
|
|
17
|
+
* @param variant - The variant snapshot to copy.
|
|
18
|
+
*
|
|
19
|
+
* @returns A defensive copy of the variant.
|
|
20
|
+
*/
|
|
21
|
+
const copyVariant = (variant) => ({
|
|
22
|
+
...variant,
|
|
23
|
+
...(variant.attributes !== undefined
|
|
24
|
+
? { attributes: variant.attributes.map((attribute) => ({ ...attribute })) }
|
|
25
|
+
: {}),
|
|
26
|
+
...(variant.options !== undefined
|
|
27
|
+
? { options: variant.options.map((option) => ({ ...option })) }
|
|
28
|
+
: {}),
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
...snapshot,
|
|
32
|
+
...(snapshot.attributes !== undefined
|
|
33
|
+
? { attributes: snapshot.attributes.map((attribute) => ({ ...attribute })) }
|
|
34
|
+
: {}),
|
|
35
|
+
...(snapshot.options !== undefined
|
|
36
|
+
? { options: snapshot.options.map((option) => ({ ...option })) }
|
|
37
|
+
: {}),
|
|
38
|
+
...(snapshot.variant !== undefined ? { variant: copyVariant(snapshot.variant) } : {}),
|
|
39
|
+
...(snapshot.metadata !== undefined ? { metadata: { ...snapshot.metadata } } : {}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns a defensive copy of a contact so later mutations of the input cannot
|
|
44
|
+
* leak into the order.
|
|
45
|
+
*
|
|
46
|
+
* @param contact - The contact to copy.
|
|
47
|
+
*
|
|
48
|
+
* @returns A defensive copy of the contact.
|
|
49
|
+
*/
|
|
50
|
+
function copyContact(contact) {
|
|
51
|
+
return { ...contact };
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns a defensive copy of the buyer fact so later mutations of the input
|
|
55
|
+
* cannot leak into the order.
|
|
56
|
+
*
|
|
57
|
+
* @param customer - The customer fact to copy.
|
|
58
|
+
*
|
|
59
|
+
* @returns A defensive copy of the customer fact.
|
|
60
|
+
*/
|
|
61
|
+
function copyCustomerSnapshot(customer) {
|
|
62
|
+
return {
|
|
63
|
+
...customer,
|
|
64
|
+
...(customer.contacts !== undefined
|
|
65
|
+
? { contacts: customer.contacts.map((contact) => copyContact(contact)) }
|
|
66
|
+
: {}),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns a defensive copy of an address fact so later mutations of the input
|
|
71
|
+
* cannot leak into the order.
|
|
72
|
+
*
|
|
73
|
+
* @param address - The address fact to copy.
|
|
74
|
+
*
|
|
75
|
+
* @returns A defensive copy of the address fact.
|
|
76
|
+
*/
|
|
77
|
+
function copyAddressSnapshot(address) {
|
|
78
|
+
return {
|
|
79
|
+
...address,
|
|
80
|
+
lines: [...address.lines],
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns whether a stored address fact and a replacement destination are
|
|
85
|
+
* structurally identical.
|
|
86
|
+
*
|
|
87
|
+
* @param address - The address fact stored on the order.
|
|
88
|
+
* @param destination - The replacement destination to compare against.
|
|
89
|
+
*
|
|
90
|
+
* @returns `true` when every field matches, otherwise `false`.
|
|
91
|
+
*/
|
|
92
|
+
function isSameShippingDestination(address, destination) {
|
|
93
|
+
return (address.addressId === destination.addressId &&
|
|
94
|
+
address.lines.length === destination.lines.length &&
|
|
95
|
+
address.lines.every((line, index) => line === destination.lines[index]) &&
|
|
96
|
+
address.city === destination.city &&
|
|
97
|
+
address.administrativeArea === destination.administrativeArea &&
|
|
98
|
+
address.postalCode === destination.postalCode &&
|
|
99
|
+
address.countryCode === destination.countryCode &&
|
|
100
|
+
address.capturedAt.epochMilliseconds === destination.capturedAt.epochMilliseconds);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Represents an order entity in the system.
|
|
104
|
+
*
|
|
105
|
+
* @remark
|
|
106
|
+
* An order is a lifecycle aggregate: it owns its status transitions, its line
|
|
107
|
+
* items, the applied pricing result, and its historical facts (buyer,
|
|
108
|
+
* addresses, payment). It does not validate coupons, does not know promotion
|
|
109
|
+
* rules, and does not orchestrate external services — those concerns belong
|
|
110
|
+
* to `@comity/pricing`, other Core Modules, and application/domain services.
|
|
111
|
+
*/
|
|
112
|
+
export class Order {
|
|
113
|
+
#id;
|
|
114
|
+
#status;
|
|
115
|
+
#items;
|
|
116
|
+
#price;
|
|
117
|
+
#channelId;
|
|
118
|
+
#customer;
|
|
119
|
+
#addresses;
|
|
120
|
+
#payments;
|
|
121
|
+
#meta;
|
|
122
|
+
#createdAt;
|
|
123
|
+
#updatedAt;
|
|
124
|
+
/**
|
|
125
|
+
* @param fields - The fields used to create or hydrate the order.
|
|
126
|
+
* @param id - The unique identifier of the order, if it has been assigned.
|
|
127
|
+
*/
|
|
128
|
+
constructor(fields, id) {
|
|
129
|
+
this.#id = id;
|
|
130
|
+
this.#status = fields.status ?? "draft";
|
|
131
|
+
this.#items = [...fields.items];
|
|
132
|
+
this.#price = fields.price;
|
|
133
|
+
this.#channelId = fields.channelId;
|
|
134
|
+
this.#customer = fields.customer !== undefined ? copyCustomerSnapshot(fields.customer) : undefined;
|
|
135
|
+
this.#addresses =
|
|
136
|
+
fields.addresses !== undefined ? fields.addresses.map((address) => copyAddressSnapshot(address)) : undefined;
|
|
137
|
+
this.#payments =
|
|
138
|
+
fields.payments !== undefined
|
|
139
|
+
? fields.payments.map((payment) => ({ ...payment }))
|
|
140
|
+
: undefined;
|
|
141
|
+
this.#meta = fields.meta ? { ...fields.meta } : undefined;
|
|
142
|
+
this.#createdAt = fields.createdAt ?? Instant.now();
|
|
143
|
+
this.#updatedAt = fields.updatedAt ?? this.#createdAt;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @returns The unique identifier of the order, if it has been assigned.
|
|
147
|
+
*/
|
|
148
|
+
get id() {
|
|
149
|
+
return this.#id;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* @returns The commercial channel through which the order was placed.
|
|
153
|
+
*/
|
|
154
|
+
get channelId() {
|
|
155
|
+
return this.#channelId;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* @returns The lifecycle status of the order.
|
|
159
|
+
*/
|
|
160
|
+
get status() {
|
|
161
|
+
return this.#status;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* @returns The order items.
|
|
165
|
+
*/
|
|
166
|
+
get items() {
|
|
167
|
+
return [...this.#items];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* @returns The order price.
|
|
171
|
+
*/
|
|
172
|
+
get price() {
|
|
173
|
+
return this.#price;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @returns The historical buyer fact, if captured.
|
|
177
|
+
*/
|
|
178
|
+
get customer() {
|
|
179
|
+
return this.#customer !== undefined
|
|
180
|
+
? {
|
|
181
|
+
...this.#customer,
|
|
182
|
+
...(this.#customer.contacts !== undefined
|
|
183
|
+
? { contacts: this.#customer.contacts.map((contact) => ({ ...contact })) }
|
|
184
|
+
: {}),
|
|
185
|
+
}
|
|
186
|
+
: undefined;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* @returns The historical address facts, if captured.
|
|
190
|
+
*/
|
|
191
|
+
get addresses() {
|
|
192
|
+
return this.#addresses !== undefined
|
|
193
|
+
? this.#addresses.map((address) => ({ ...address, lines: [...address.lines] }))
|
|
194
|
+
: undefined;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* @returns The historical payment facts, if attached.
|
|
198
|
+
*/
|
|
199
|
+
get payments() {
|
|
200
|
+
return this.#payments?.map((payment) => ({ ...payment }));
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* @returns The custom metadata of the order, if any.
|
|
204
|
+
*/
|
|
205
|
+
get meta() {
|
|
206
|
+
return this.#meta ? { ...this.#meta } : undefined;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @returns The timestamp when the order was created.
|
|
210
|
+
*/
|
|
211
|
+
get createdAt() {
|
|
212
|
+
return this.#createdAt;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* @returns The timestamp when the order was last updated.
|
|
216
|
+
*/
|
|
217
|
+
get updatedAt() {
|
|
218
|
+
return this.#updatedAt;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Updates the applied pricing result and metadata.
|
|
222
|
+
*
|
|
223
|
+
* Status transitions are performed through the domain methods; items are
|
|
224
|
+
* mutated through `addItem`/`removeItem`/`updateItemQuantity`.
|
|
225
|
+
*
|
|
226
|
+
* @param changes - The changes to apply to the order.
|
|
227
|
+
*/
|
|
228
|
+
update(changes) {
|
|
229
|
+
if (changes.price !== undefined) {
|
|
230
|
+
this.#price = changes.price;
|
|
231
|
+
}
|
|
232
|
+
if (changes.meta !== undefined) {
|
|
233
|
+
this.#meta = { ...changes.meta };
|
|
234
|
+
}
|
|
235
|
+
this.#updatedAt = Instant.now();
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Appends a historical payment fact to the order.
|
|
239
|
+
*
|
|
240
|
+
* A payment may be processed after the order is created, so the outcome is
|
|
241
|
+
* recorded through a dedicated method rather than the generic update path.
|
|
242
|
+
* This records a historical fact only: it performs no payment logic, knows
|
|
243
|
+
* no payment module, and does not coordinate, authorize, or capture.
|
|
244
|
+
*
|
|
245
|
+
* @param snapshot - The payment fact to record.
|
|
246
|
+
*/
|
|
247
|
+
attachPayment(snapshot) {
|
|
248
|
+
this.#payments = [
|
|
249
|
+
...(this.#payments ?? []),
|
|
250
|
+
{ ...snapshot },
|
|
251
|
+
];
|
|
252
|
+
this.#updatedAt = Instant.now();
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Replaces the shipping destination of the order.
|
|
256
|
+
*
|
|
257
|
+
* Allowed only while the order is `draft | pending`; in later statuses the
|
|
258
|
+
* shipping destination is immutable (ADR-018). The order must hold exactly
|
|
259
|
+
* one shipping address fact; a missing or ambiguous one is an error rather
|
|
260
|
+
* than a guess. A structurally identical destination is a successful no-op
|
|
261
|
+
* that leaves timestamps untouched. The operation never reaches the generic
|
|
262
|
+
* `update()` path, never touches billing, customer, item, price, or payment
|
|
263
|
+
* facts, and records no history: the aggregate retains only the current
|
|
264
|
+
* shipping destination.
|
|
265
|
+
*
|
|
266
|
+
* @param destination - The replacement shipping destination. The address
|
|
267
|
+
* role is owned by this operation and is always stored as `"shipping"`.
|
|
268
|
+
*
|
|
269
|
+
* @returns A result indicating the success or failure of the change.
|
|
270
|
+
*/
|
|
271
|
+
changeShippingDestination(destination) {
|
|
272
|
+
if (this.#status !== "draft" && this.#status !== "pending") {
|
|
273
|
+
return failure(new OrderError("shipping_destination_immutable", {
|
|
274
|
+
details: {
|
|
275
|
+
...(this.#id !== undefined ? { orderId: this.#id.toString() } : {}),
|
|
276
|
+
},
|
|
277
|
+
}));
|
|
278
|
+
}
|
|
279
|
+
const addresses = this.#addresses;
|
|
280
|
+
const shipping = (addresses ?? []).filter((address) => address.role === "shipping");
|
|
281
|
+
const current = shipping.length === 1 ? shipping[0] : undefined;
|
|
282
|
+
if (current === undefined || addresses === undefined) {
|
|
283
|
+
return failure(new OrderError("ambiguous_shipping_destination", {
|
|
284
|
+
details: {
|
|
285
|
+
...(this.#id !== undefined ? { orderId: this.#id.toString() } : {}),
|
|
286
|
+
},
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
if (isSameShippingDestination(current, destination)) {
|
|
290
|
+
return success(undefined);
|
|
291
|
+
}
|
|
292
|
+
this.#addresses = addresses.map((address) => address.role === "shipping" ? copyAddressSnapshot({ ...destination, role: "shipping" }) : address);
|
|
293
|
+
this.#updatedAt = Instant.now();
|
|
294
|
+
return success(undefined);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Adds an item to the order.
|
|
298
|
+
*
|
|
299
|
+
* @param input - The item data to add.
|
|
300
|
+
*
|
|
301
|
+
* @returns The created item, or an `invalid_quantity` error when the
|
|
302
|
+
* quantity is not a positive integer.
|
|
303
|
+
*/
|
|
304
|
+
addItem(input) {
|
|
305
|
+
if (!Number.isInteger(input.quantity) || input.quantity < 1) {
|
|
306
|
+
return failure(new OrderError("invalid_quantity", {
|
|
307
|
+
details: {
|
|
308
|
+
...(this.#id !== undefined ? { orderId: this.#id.toString() } : {}),
|
|
309
|
+
field: "quantity",
|
|
310
|
+
},
|
|
311
|
+
}));
|
|
312
|
+
}
|
|
313
|
+
const item = {
|
|
314
|
+
id: crypto.randomUUID(),
|
|
315
|
+
product: copyProductSnapshot(input.product),
|
|
316
|
+
quantity: input.quantity,
|
|
317
|
+
price: input.price,
|
|
318
|
+
};
|
|
319
|
+
this.#items.push(item);
|
|
320
|
+
this.#updatedAt = Instant.now();
|
|
321
|
+
return success(item);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Removes an item from the order.
|
|
325
|
+
*
|
|
326
|
+
* @param itemId - The order item ID.
|
|
327
|
+
*
|
|
328
|
+
* @returns A result indicating the success or failure of the removal.
|
|
329
|
+
*/
|
|
330
|
+
removeItem(itemId) {
|
|
331
|
+
const index = this.#items.findIndex((item) => item.id === itemId);
|
|
332
|
+
if (index === -1) {
|
|
333
|
+
return failure(new OrderError("invalid_item", {
|
|
334
|
+
details: {
|
|
335
|
+
...(this.#id !== undefined ? { orderId: this.#id.toString() } : {}),
|
|
336
|
+
itemId,
|
|
337
|
+
},
|
|
338
|
+
}));
|
|
339
|
+
}
|
|
340
|
+
this.#items.splice(index, 1);
|
|
341
|
+
this.#updatedAt = Instant.now();
|
|
342
|
+
return success(undefined);
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Updates the quantity of an order item.
|
|
346
|
+
*
|
|
347
|
+
* @param itemId - The order item ID.
|
|
348
|
+
* @param quantity - The new quantity.
|
|
349
|
+
*
|
|
350
|
+
* @returns A result indicating the success or failure of the update.
|
|
351
|
+
*/
|
|
352
|
+
updateItemQuantity(itemId, quantity) {
|
|
353
|
+
if (!Number.isInteger(quantity) || quantity < 1) {
|
|
354
|
+
return failure(new OrderError("invalid_quantity", {
|
|
355
|
+
details: {
|
|
356
|
+
...(this.#id !== undefined ? { orderId: this.#id.toString() } : {}),
|
|
357
|
+
itemId,
|
|
358
|
+
field: "quantity",
|
|
359
|
+
},
|
|
360
|
+
}));
|
|
361
|
+
}
|
|
362
|
+
const index = this.#items.findIndex((candidate) => candidate.id === itemId);
|
|
363
|
+
if (index === -1) {
|
|
364
|
+
return failure(new OrderError("invalid_item", {
|
|
365
|
+
details: {
|
|
366
|
+
...(this.#id !== undefined ? { orderId: this.#id.toString() } : {}),
|
|
367
|
+
itemId,
|
|
368
|
+
},
|
|
369
|
+
}));
|
|
370
|
+
}
|
|
371
|
+
const current = this.#items[index];
|
|
372
|
+
if (current !== undefined) {
|
|
373
|
+
this.#items[index] = { ...current, quantity };
|
|
374
|
+
this.#updatedAt = Instant.now();
|
|
375
|
+
}
|
|
376
|
+
return success(undefined);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Submits the order: `draft` → `pending`.
|
|
380
|
+
*
|
|
381
|
+
* @returns A result indicating the success or failure of the transition.
|
|
382
|
+
*/
|
|
383
|
+
submit() {
|
|
384
|
+
return this.#transition("pending");
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Confirms the order: `pending` → `confirmed`.
|
|
388
|
+
*
|
|
389
|
+
* @returns A result indicating the success or failure of the transition.
|
|
390
|
+
*/
|
|
391
|
+
confirm() {
|
|
392
|
+
return this.#transition("confirmed");
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Fulfills the order: `confirmed` → `fulfilled`.
|
|
396
|
+
*
|
|
397
|
+
* @returns A result indicating the success or failure of the transition.
|
|
398
|
+
*/
|
|
399
|
+
fulfill() {
|
|
400
|
+
return this.#transition("fulfilled");
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Cancels the order: `draft | pending | confirmed` → `cancelled`.
|
|
404
|
+
*
|
|
405
|
+
* @returns A result indicating the success or failure of the transition.
|
|
406
|
+
*/
|
|
407
|
+
cancel() {
|
|
408
|
+
return this.#transition("cancelled");
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Creates a snapshot of the current state of the order.
|
|
412
|
+
* Requires the order to have an assigned identifier.
|
|
413
|
+
*
|
|
414
|
+
* @returns A snapshot representing the current state of the order.
|
|
415
|
+
*/
|
|
416
|
+
snapshot() {
|
|
417
|
+
return {
|
|
418
|
+
id: this.#id,
|
|
419
|
+
status: this.#status,
|
|
420
|
+
items: [...this.#items],
|
|
421
|
+
price: this.#price,
|
|
422
|
+
channelId: this.#channelId,
|
|
423
|
+
...(this.#customer !== undefined ? { customer: copyCustomerSnapshot(this.#customer) } : {}),
|
|
424
|
+
...(this.#addresses !== undefined
|
|
425
|
+
? { addresses: this.#addresses.map((address) => copyAddressSnapshot(address)) }
|
|
426
|
+
: {}),
|
|
427
|
+
...(this.#payments !== undefined
|
|
428
|
+
? {
|
|
429
|
+
payments: this.#payments.map((payment) => ({ ...payment })),
|
|
430
|
+
}
|
|
431
|
+
: {}),
|
|
432
|
+
...(this.#meta !== undefined ? { meta: { ...this.#meta } } : {}),
|
|
433
|
+
createdAt: this.#createdAt,
|
|
434
|
+
updatedAt: this.#updatedAt,
|
|
435
|
+
capturedAt: Instant.now(),
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Applies a lifecycle transition through the centralized transition rules.
|
|
440
|
+
*
|
|
441
|
+
* @param to - The target status.
|
|
442
|
+
*
|
|
443
|
+
* @returns A result indicating the success or failure of the transition.
|
|
444
|
+
*/
|
|
445
|
+
#transition(to) {
|
|
446
|
+
const result = transitionOrderStatus(this.#status, to);
|
|
447
|
+
if (!result.success) {
|
|
448
|
+
return result;
|
|
449
|
+
}
|
|
450
|
+
this.#status = to;
|
|
451
|
+
this.#updatedAt = Instant.now();
|
|
452
|
+
return success(undefined);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
//# sourceMappingURL=order.js.map
|