@jarwizz/create-jarshop 0.1.9 → 0.4.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/README.md +15 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1 -0
- package/dist/cli.js.map +1 -1
- package/dist/generator.d.ts +11 -1
- package/dist/generator.js +45 -11
- package/dist/generator.js.map +1 -1
- package/dist/presets.d.ts +28 -0
- package/dist/presets.js +105 -0
- package/dist/presets.js.map +1 -0
- package/dist/template/CONTEXT.md +4 -2
- package/dist/template/README.md +255 -3
- package/dist/template/env.example.template +19 -0
- package/dist/template/gitignore.template +1 -0
- package/dist/template/jarshop.config.ts +37 -0
- package/dist/template/package.json +4 -0
- package/dist/template/pnpm-lock.yaml +3284 -1815
- package/dist/template/pnpm-workspace.yaml +6 -1
- package/dist/template/src/client/features.ts +41 -0
- package/dist/template/src/client/invoicing.ts +29 -0
- package/dist/template/src/dev-storefront-environment.test.ts +45 -0
- package/dist/template/src/dev-storefront-environment.ts +32 -0
- package/dist/template/src/dev-storefront.ts +3 -12
- package/dist/template/src/environment.d.ts +8 -0
- package/dist/template/src/invoice-email-recover.ts +59 -0
- package/dist/template/src/jarshop-config-loader.ts +118 -0
- package/dist/template/src/jarshop-config.test.ts +365 -0
- package/dist/template/src/jarshop-config.ts +462 -0
- package/dist/template/src/jarshop-features.test.ts +222 -0
- package/dist/template/src/jarshop-features.ts +112 -0
- package/dist/template/src/migrations/1788100000000-jarshop-guest-order-session.ts +27 -0
- package/dist/template/src/operation-codes.ts +34 -0
- package/dist/template/src/packeta-config.test.ts +95 -0
- package/dist/template/src/packeta-countries.test.ts +161 -0
- package/dist/template/src/packeta-countries.ts +120 -0
- package/dist/template/src/packeta-validation.test.ts +35 -0
- package/dist/template/src/packeta-validation.ts +52 -0
- package/dist/template/src/payment-return-url.test.ts +57 -0
- package/dist/template/src/payment-return-url.ts +34 -0
- package/dist/template/src/plugins/jarshop-checkout/confirmation-view.test.ts +79 -0
- package/dist/template/src/plugins/jarshop-checkout/confirmation.ts +51 -0
- package/dist/template/src/plugins/jarshop-checkout/dashboard/index.tsx +1 -0
- package/dist/template/src/plugins/jarshop-checkout/index.ts +4 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-delivery.plugin.ts +79 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email-events.ts +35 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email-lifecycle.ts +235 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email-worker.test.ts +426 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email.test.ts +107 -0
- package/dist/template/src/plugins/jarshop-checkout/jarshop-checkout.plugin.ts +167 -58
- package/dist/template/src/plugins/jarshop-checkout/market-active-order-core.ts +108 -0
- package/dist/template/src/plugins/jarshop-checkout/market-active-order.test.ts +205 -0
- package/dist/template/src/plugins/jarshop-checkout/market-active-order.ts +186 -0
- package/dist/template/src/plugins/jarshop-checkout/market-channels.test.ts +58 -0
- package/dist/template/src/plugins/jarshop-checkout/market-channels.ts +87 -0
- package/dist/template/src/plugins/jarshop-checkout/operation-code-check.ts +36 -0
- package/dist/template/src/plugins/jarshop-checkout/order-email-configuration.entity.ts +1 -1
- package/dist/template/src/plugins/jarshop-checkout/order-email-configuration.ts +1 -0
- package/dist/template/src/plugins/jarshop-checkout/order-email-delivery-core.ts +94 -1
- package/dist/template/src/plugins/jarshop-checkout/order-email-delivery.entity.ts +1 -0
- package/dist/template/src/plugins/jarshop-checkout/order-email-outbox.ts +83 -1
- package/dist/template/src/plugins/jarshop-checkout/order-email-worker.ts +136 -1
- package/dist/template/src/plugins/jarshop-checkout/shipping-tax.test.ts +109 -0
- package/dist/template/src/plugins/jarshop-checkout/shipping.test.ts +156 -0
- package/dist/template/src/plugins/jarshop-checkout/shipping.ts +75 -11
- package/dist/template/src/seed.ts +473 -185
- package/dist/template/src/vendure-config.ts +258 -113
- package/dist/template/storefront/README.md +7 -0
- package/dist/template/storefront/app/[locale]/[slug]/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/login/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/page.tsx +10 -4
- package/dist/template/storefront/app/[locale]/account/password-reset/callback/route.ts +4 -2
- package/dist/template/storefront/app/[locale]/account/password-reset/complete/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/password-reset/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/register/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/verify/complete/route.ts +4 -2
- package/dist/template/storefront/app/[locale]/account/verify/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/cart/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/checkout/actions.ts +43 -0
- package/dist/template/storefront/app/[locale]/checkout/checkout-form.tsx +188 -36
- package/dist/template/storefront/app/[locale]/checkout/confirmation/route.ts +2 -2
- package/dist/template/storefront/app/[locale]/checkout/packeta-chooser.tsx +96 -0
- package/dist/template/storefront/app/[locale]/checkout/page.tsx +60 -2
- package/dist/template/storefront/app/[locale]/checkout/payment-return/actions.ts +55 -0
- package/dist/template/storefront/app/[locale]/checkout/payment-return/page.tsx +32 -0
- package/dist/template/storefront/app/[locale]/checkout/payment-return/payment-return.tsx +84 -0
- package/dist/template/storefront/app/[locale]/checkout/success/page.tsx +32 -2
- package/dist/template/storefront/app/[locale]/layout.tsx +3 -2
- package/dist/template/storefront/app/[locale]/page.tsx +3 -2
- package/dist/template/storefront/app/globals.css +16 -0
- package/dist/template/storefront/app/layout.tsx +6 -2
- package/dist/template/storefront/lib/cart.ts +2 -4
- package/dist/template/storefront/lib/checkout-contract.test.ts +18 -1
- package/dist/template/storefront/lib/checkout-contract.ts +8 -0
- package/dist/template/storefront/lib/checkout-countries.test.ts +61 -0
- package/dist/template/storefront/lib/checkout-countries.ts +67 -0
- package/dist/template/storefront/lib/checkout.ts +22 -11
- package/dist/template/storefront/lib/commerce.ts +262 -2
- package/dist/template/storefront/lib/confirmation.test.ts +116 -0
- package/dist/template/storefront/lib/confirmation.ts +13 -7
- package/dist/template/storefront/lib/i18n.test.ts +32 -12
- package/dist/template/storefront/lib/i18n.ts +29 -8
- package/dist/template/storefront/lib/market-config.ts +40 -0
- package/dist/template/storefront/lib/market-routing.test.ts +107 -0
- package/dist/template/storefront/lib/market-routing.ts +57 -0
- package/dist/template/storefront/lib/market.ts +51 -0
- package/dist/template/storefront/lib/packeta-selection.test.ts +141 -0
- package/dist/template/storefront/lib/packeta-selection.ts +98 -0
- package/dist/template/storefront/lib/payment-return-actions.test.ts +61 -0
- package/dist/template/storefront/lib/payment-return-page.test.ts +30 -0
- package/dist/template/storefront/lib/payment-return-polling.test.ts +37 -0
- package/dist/template/storefront/lib/payment-return-polling.ts +58 -0
- package/dist/template/storefront/messages/en.json +13 -1
- package/dist/template/storefront/messages/sk.json +13 -1
- package/dist/template/storefront/package.json +2 -1
- package/dist/template/storefront/proxy.ts +35 -0
- package/dist/template/turbo.json +5 -1
- package/dist/template-manifest.json +1 -1
- package/dist/template.js +1 -0
- package/dist/template.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createMarketActiveOrder,
|
|
5
|
+
determineMarketActiveOrder,
|
|
6
|
+
type MarketOrder,
|
|
7
|
+
type MarketOrderContext,
|
|
8
|
+
type MarketOrderStore,
|
|
9
|
+
} from "./market-active-order-core.js";
|
|
10
|
+
|
|
11
|
+
interface FakeOrder extends MarketOrder {
|
|
12
|
+
active: boolean;
|
|
13
|
+
channelId: string;
|
|
14
|
+
userId?: string;
|
|
15
|
+
guestSessionId?: string;
|
|
16
|
+
updatedAt: number;
|
|
17
|
+
}
|
|
18
|
+
interface FakeContext extends MarketOrderContext {
|
|
19
|
+
session: { id: string; activeOrderId?: string | undefined };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Mimics Vendure: orders live in one Channel, the session may point at any. */
|
|
23
|
+
function fakeShop() {
|
|
24
|
+
const orders: FakeOrder[] = [];
|
|
25
|
+
let sequence = 0;
|
|
26
|
+
let clock = 0;
|
|
27
|
+
const store: MarketOrderStore<FakeContext, FakeOrder> = {
|
|
28
|
+
findInChannel: async (ctx, id) =>
|
|
29
|
+
orders.find((o) => o.id === id && o.channelId === String(ctx.channelId)),
|
|
30
|
+
findActiveForUser: async (ctx, userId) =>
|
|
31
|
+
orders.find(
|
|
32
|
+
(o) =>
|
|
33
|
+
o.active &&
|
|
34
|
+
o.userId === String(userId) &&
|
|
35
|
+
o.channelId === String(ctx.channelId),
|
|
36
|
+
),
|
|
37
|
+
findActiveForGuestSession: async (ctx, sessionId) =>
|
|
38
|
+
orders
|
|
39
|
+
.filter(
|
|
40
|
+
(o) =>
|
|
41
|
+
o.active &&
|
|
42
|
+
o.channelId === String(ctx.channelId) &&
|
|
43
|
+
o.guestSessionId === sessionId,
|
|
44
|
+
)
|
|
45
|
+
.sort((a, b) => b.updatedAt - a.updatedAt)[0],
|
|
46
|
+
unsetSessionOrder: async (ctx) => {
|
|
47
|
+
ctx.session.activeOrderId = undefined;
|
|
48
|
+
},
|
|
49
|
+
create: async (ctx, userId) => {
|
|
50
|
+
const order: FakeOrder = {
|
|
51
|
+
id: `order-${++sequence}`,
|
|
52
|
+
active: true,
|
|
53
|
+
channelId: String(ctx.channelId),
|
|
54
|
+
updatedAt: ++clock,
|
|
55
|
+
...(userId === undefined ? {} : { userId: String(userId) }),
|
|
56
|
+
};
|
|
57
|
+
orders.push(order);
|
|
58
|
+
return order;
|
|
59
|
+
},
|
|
60
|
+
stampGuestSession: async (_ctx, order, sessionId) => {
|
|
61
|
+
order.guestSessionId = sessionId;
|
|
62
|
+
return order;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
/** Vendure's ActiveOrderService stores the strategy's result on the session. */
|
|
66
|
+
async function getActiveOrder(ctx: FakeContext, createIfNotExists: boolean) {
|
|
67
|
+
let order = await determineMarketActiveOrder(store, ctx);
|
|
68
|
+
if (!order && createIfNotExists)
|
|
69
|
+
order = await createMarketActiveOrder(store, ctx);
|
|
70
|
+
if (order) ctx.session.activeOrderId = String(order.id);
|
|
71
|
+
return order;
|
|
72
|
+
}
|
|
73
|
+
const context = (
|
|
74
|
+
channelId: string,
|
|
75
|
+
session: FakeContext["session"],
|
|
76
|
+
activeUserId?: string,
|
|
77
|
+
): FakeContext => ({ channelId, session, activeUserId });
|
|
78
|
+
return { orders, store, getActiveOrder, context };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
describe("market-scoped active Order", () => {
|
|
82
|
+
it("reuses the session's cart inside one market", async () => {
|
|
83
|
+
const shop = fakeShop();
|
|
84
|
+
const session = { id: "s1" };
|
|
85
|
+
const first = await shop.getActiveOrder(shop.context("sk", session), true);
|
|
86
|
+
const again = await shop.getActiveOrder(shop.context("sk", session), true);
|
|
87
|
+
expect(again?.id).toBe(first?.id);
|
|
88
|
+
expect(shop.orders).toHaveLength(1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("returns the original guest cart after SK -> CZ -> SK on one session", async () => {
|
|
92
|
+
const shop = fakeShop();
|
|
93
|
+
const session = { id: "s1" };
|
|
94
|
+
const sk = await shop.getActiveOrder(shop.context("sk", session), true);
|
|
95
|
+
const cz = await shop.getActiveOrder(shop.context("cz", session), true);
|
|
96
|
+
expect(cz?.id).not.toBe(sk?.id);
|
|
97
|
+
const backInSk = await shop.getActiveOrder(
|
|
98
|
+
shop.context("sk", session),
|
|
99
|
+
true,
|
|
100
|
+
);
|
|
101
|
+
expect(backInSk?.id).toBe(sk?.id);
|
|
102
|
+
const backInCz = await shop.getActiveOrder(
|
|
103
|
+
shop.context("cz", session),
|
|
104
|
+
true,
|
|
105
|
+
);
|
|
106
|
+
expect(backInCz?.id).toBe(cz?.id);
|
|
107
|
+
expect(shop.orders).toHaveLength(2);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("does not show a guest cart to another session", async () => {
|
|
111
|
+
const shop = fakeShop();
|
|
112
|
+
const owner = await shop.getActiveOrder(
|
|
113
|
+
shop.context("sk", { id: "s1" }),
|
|
114
|
+
true,
|
|
115
|
+
);
|
|
116
|
+
const other = await shop.getActiveOrder(
|
|
117
|
+
shop.context("sk", { id: "s2" }),
|
|
118
|
+
false,
|
|
119
|
+
);
|
|
120
|
+
expect(other).toBeUndefined();
|
|
121
|
+
const stranger = await shop.getActiveOrder(
|
|
122
|
+
shop.context("sk", { id: "s2" }),
|
|
123
|
+
true,
|
|
124
|
+
);
|
|
125
|
+
expect(stranger?.id).not.toBe(owner?.id);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("ignores inactive guest carts and detaches a stale session order", async () => {
|
|
129
|
+
const shop = fakeShop();
|
|
130
|
+
const session: FakeContext["session"] = { id: "s1" };
|
|
131
|
+
const placed = await shop.getActiveOrder(shop.context("sk", session), true);
|
|
132
|
+
placed!.active = false;
|
|
133
|
+
const ctx = shop.context("sk", session);
|
|
134
|
+
expect(await determineMarketActiveOrder(shop.store, ctx)).toBeUndefined();
|
|
135
|
+
expect(ctx.session.activeOrderId).toBeUndefined();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("prefers the most recently updated guest cart", async () => {
|
|
139
|
+
const shop = fakeShop();
|
|
140
|
+
const older = await shop.getActiveOrder(
|
|
141
|
+
shop.context("sk", { id: "s1" }),
|
|
142
|
+
true,
|
|
143
|
+
);
|
|
144
|
+
const newer = await shop.getActiveOrder(
|
|
145
|
+
shop.context("sk", { id: "s1" }),
|
|
146
|
+
true,
|
|
147
|
+
);
|
|
148
|
+
expect(newer?.id).toBe(older?.id);
|
|
149
|
+
// A second stamped cart (e.g. created by a concurrent request) wins by recency.
|
|
150
|
+
const extra = await createMarketActiveOrder(
|
|
151
|
+
shop.store,
|
|
152
|
+
shop.context("sk", { id: "s1" }),
|
|
153
|
+
);
|
|
154
|
+
const found = await determineMarketActiveOrder(
|
|
155
|
+
shop.store,
|
|
156
|
+
shop.context("sk", { id: "s1" }),
|
|
157
|
+
);
|
|
158
|
+
expect(found?.id).toBe(extra.id);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("finds a signed-in user's cart per market without the guest stamp", async () => {
|
|
162
|
+
const shop = fakeShop();
|
|
163
|
+
const session = { id: "s1" };
|
|
164
|
+
const sk = await shop.getActiveOrder(
|
|
165
|
+
shop.context("sk", session, "u1"),
|
|
166
|
+
true,
|
|
167
|
+
);
|
|
168
|
+
const cz = await shop.getActiveOrder(
|
|
169
|
+
shop.context("cz", session, "u1"),
|
|
170
|
+
true,
|
|
171
|
+
);
|
|
172
|
+
expect(cz?.id).not.toBe(sk?.id);
|
|
173
|
+
expect(
|
|
174
|
+
(await shop.getActiveOrder(shop.context("sk", session, "u1"), true))?.id,
|
|
175
|
+
).toBe(sk?.id);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("stamps the session id on guest carts only", async () => {
|
|
179
|
+
const shop = fakeShop();
|
|
180
|
+
const guest = await shop.getActiveOrder(
|
|
181
|
+
shop.context("sk", { id: "s1" }),
|
|
182
|
+
true,
|
|
183
|
+
);
|
|
184
|
+
const user = await shop.getActiveOrder(
|
|
185
|
+
shop.context("sk", { id: "s2" }, "u1"),
|
|
186
|
+
true,
|
|
187
|
+
);
|
|
188
|
+
expect(shop.orders.find((o) => o.id === guest?.id)?.guestSessionId).toBe(
|
|
189
|
+
"s1",
|
|
190
|
+
);
|
|
191
|
+
expect(
|
|
192
|
+
shop.orders.find((o) => o.id === user?.id)?.guestSessionId,
|
|
193
|
+
).toBeUndefined();
|
|
194
|
+
expect(shop.orders.find((o) => o.id === user?.id)?.userId).toBe("u1");
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("requires a session", async () => {
|
|
198
|
+
const shop = fakeShop();
|
|
199
|
+
await expect(
|
|
200
|
+
determineMarketActiveOrder(shop.store, {
|
|
201
|
+
channelId: "sk",
|
|
202
|
+
} as FakeContext),
|
|
203
|
+
).rejects.toThrow("session");
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ActiveOrderStrategy,
|
|
3
|
+
DefaultActiveOrderStrategy,
|
|
4
|
+
type CustomFieldConfig,
|
|
5
|
+
CustomerService,
|
|
6
|
+
type Injector,
|
|
7
|
+
Order,
|
|
8
|
+
OrderService,
|
|
9
|
+
type RequestContext,
|
|
10
|
+
SessionService,
|
|
11
|
+
TransactionalConnection,
|
|
12
|
+
type VendureConfig,
|
|
13
|
+
} from "@vendure/core";
|
|
14
|
+
import { DEFAULT_CHANNEL_CODE } from "@vendure/common/lib/shared-constants";
|
|
15
|
+
import { Index, type SelectQueryBuilder } from "typeorm";
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
GUEST_SESSION_FIELD,
|
|
19
|
+
createMarketActiveOrder,
|
|
20
|
+
determineMarketActiveOrder,
|
|
21
|
+
type MarketOrderStore,
|
|
22
|
+
} from "./market-active-order-core.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Internal Order custom field. `internal` keeps it out of both GraphQL APIs;
|
|
26
|
+
* it only ties a guest cart to the session that created it. The matching
|
|
27
|
+
* column and index are created by the `JarShopGuestOrderSession` migration.
|
|
28
|
+
*/
|
|
29
|
+
export const guestSessionCustomField: CustomFieldConfig = {
|
|
30
|
+
name: GUEST_SESSION_FIELD,
|
|
31
|
+
type: "string",
|
|
32
|
+
nullable: true,
|
|
33
|
+
internal: true,
|
|
34
|
+
readonly: true,
|
|
35
|
+
public: false,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Keeps one cart per market on a session that is used across markets: guest
|
|
40
|
+
* carts through the session stamp, signed-in carts through the Customer, both
|
|
41
|
+
* limited to Orders that belong to the request's market.
|
|
42
|
+
*/
|
|
43
|
+
export class JarShopMarketActiveOrderStrategy implements ActiveOrderStrategy {
|
|
44
|
+
readonly name = "jarshop-market-active-order";
|
|
45
|
+
|
|
46
|
+
private connection!: TransactionalConnection;
|
|
47
|
+
private orderService!: OrderService;
|
|
48
|
+
private customerService!: CustomerService;
|
|
49
|
+
private sessionService!: SessionService;
|
|
50
|
+
/**
|
|
51
|
+
* Orders of the context's market. Vendure also assigns every Order to the
|
|
52
|
+
* default Channel, so a market's cart is visible in the default market's
|
|
53
|
+
* Channel too; an Order that belongs to another market's Channel is not a
|
|
54
|
+
* cart of this one.
|
|
55
|
+
*/
|
|
56
|
+
private marketOrders(ctx: RequestContext): SelectQueryBuilder<Order> {
|
|
57
|
+
return this.connection
|
|
58
|
+
.getRepository(ctx, Order)
|
|
59
|
+
.createQueryBuilder("order")
|
|
60
|
+
.innerJoin("order.channels", "channel", "channel.id = :channelId", {
|
|
61
|
+
channelId: ctx.channelId,
|
|
62
|
+
})
|
|
63
|
+
.andWhere(
|
|
64
|
+
`NOT EXISTS (
|
|
65
|
+
SELECT 1 FROM "order_channels_channel" "other_link"
|
|
66
|
+
INNER JOIN "channel" "other_channel"
|
|
67
|
+
ON "other_channel"."id" = "other_link"."channelId"
|
|
68
|
+
WHERE "other_link"."orderId" = "order"."id"
|
|
69
|
+
AND "other_link"."channelId" <> :channelId
|
|
70
|
+
AND "other_channel"."code" <> :defaultChannelCode
|
|
71
|
+
)`,
|
|
72
|
+
{ channelId: ctx.channelId, defaultChannelCode: DEFAULT_CHANNEL_CODE },
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private readonly store: MarketOrderStore<RequestContext, Order> = {
|
|
77
|
+
findInChannel: (ctx, orderId) =>
|
|
78
|
+
this.marketOrders(ctx)
|
|
79
|
+
.andWhere("order.id = :orderId", { orderId })
|
|
80
|
+
.getOne()
|
|
81
|
+
.then((order) => order ?? undefined),
|
|
82
|
+
findActiveForUser: async (ctx, userId) => {
|
|
83
|
+
const customer = await this.customerService.findOneByUserId(ctx, userId);
|
|
84
|
+
if (!customer) return undefined;
|
|
85
|
+
const match = await this.marketOrders(ctx)
|
|
86
|
+
.andWhere("order.active = :active", { active: true })
|
|
87
|
+
.andWhere("order.customerId = :customerId", {
|
|
88
|
+
customerId: customer.id,
|
|
89
|
+
})
|
|
90
|
+
.orderBy("order.createdAt", "DESC")
|
|
91
|
+
.getOne();
|
|
92
|
+
return match ? this.orderService.findOne(ctx, match.id) : undefined;
|
|
93
|
+
},
|
|
94
|
+
findActiveForGuestSession: async (ctx, sessionId) => {
|
|
95
|
+
const match = await this.marketOrders(ctx)
|
|
96
|
+
.andWhere("order.active = :active", { active: true })
|
|
97
|
+
.andWhere(`order.customFields.${GUEST_SESSION_FIELD} = :sessionId`, {
|
|
98
|
+
sessionId,
|
|
99
|
+
})
|
|
100
|
+
.orderBy("order.updatedAt", "DESC")
|
|
101
|
+
.getOne();
|
|
102
|
+
return match ? this.orderService.findOne(ctx, match.id) : undefined;
|
|
103
|
+
},
|
|
104
|
+
unsetSessionOrder: async (ctx) => {
|
|
105
|
+
if (ctx.session)
|
|
106
|
+
await this.sessionService.unsetActiveOrder(ctx, ctx.session);
|
|
107
|
+
},
|
|
108
|
+
create: (ctx, userId) => this.orderService.create(ctx, userId),
|
|
109
|
+
stampGuestSession: async (ctx, order, sessionId) => {
|
|
110
|
+
await this.connection
|
|
111
|
+
.getRepository(ctx, Order)
|
|
112
|
+
.createQueryBuilder()
|
|
113
|
+
.update(Order)
|
|
114
|
+
.set({ customFields: { [GUEST_SESSION_FIELD]: sessionId } })
|
|
115
|
+
.where("id = :id", { id: order.id })
|
|
116
|
+
.execute();
|
|
117
|
+
return order;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
init(injector: Injector): void {
|
|
122
|
+
this.connection = injector.get(TransactionalConnection);
|
|
123
|
+
this.orderService = injector.get(OrderService);
|
|
124
|
+
this.customerService = injector.get(CustomerService);
|
|
125
|
+
// Vendure's own `getActiveOrderForUser` is also what sign-in uses to pick
|
|
126
|
+
// the Order a guest cart merges into. In the default Channel it would pick
|
|
127
|
+
// the newest cart of ANY market and merge the guest cart into it, mixing
|
|
128
|
+
// currencies. Scope it to the context's market like the strategy above.
|
|
129
|
+
this.orderService.getActiveOrderForUser = (ctx, userId) =>
|
|
130
|
+
this.store.findActiveForUser(ctx, userId);
|
|
131
|
+
this.sessionService = injector.get(SessionService);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
determineActiveOrder(ctx: RequestContext): Promise<Order | undefined> {
|
|
135
|
+
return determineMarketActiveOrder(this.store, ctx);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
createActiveOrder(ctx: RequestContext): Promise<Order> {
|
|
139
|
+
return createMarketActiveOrder(this.store, ctx);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const GUEST_SESSION_INDEX = "IDX_jarshop_order_guest_session";
|
|
144
|
+
let guestSessionIndexDeclared = false;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Vendure only supports a unique index on a custom field. The guest lookup
|
|
148
|
+
* runs for every anonymous cart request, so declare a partial index on the
|
|
149
|
+
* Order entity metadata; the migration creates the same index, which keeps
|
|
150
|
+
* TypeORM's schema comparison (and Vendure's migration warning) clean.
|
|
151
|
+
*/
|
|
152
|
+
function declareGuestSessionIndex(): void {
|
|
153
|
+
if (guestSessionIndexDeclared) return;
|
|
154
|
+
guestSessionIndexDeclared = true;
|
|
155
|
+
Index(GUEST_SESSION_INDEX, [`customFields.${GUEST_SESSION_FIELD}`], {
|
|
156
|
+
where: `"customFieldsJarshopguestsessionid" IS NOT NULL`,
|
|
157
|
+
})(Order);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Registers the Order custom field and replaces the default strategy. */
|
|
161
|
+
export function registerJarShopMarketActiveOrder<Config extends VendureConfig>(
|
|
162
|
+
config: Config,
|
|
163
|
+
): Config {
|
|
164
|
+
declareGuestSessionIndex();
|
|
165
|
+
config.customFields ??= {};
|
|
166
|
+
const orderFields = (config.customFields.Order ??= []);
|
|
167
|
+
if (!orderFields.some((field) => field.name === GUEST_SESSION_FIELD)) {
|
|
168
|
+
orderFields.push(guestSessionCustomField);
|
|
169
|
+
}
|
|
170
|
+
config.orderOptions ??= {};
|
|
171
|
+
const current = config.orderOptions.activeOrderStrategy;
|
|
172
|
+
const strategies = (
|
|
173
|
+
Array.isArray(current) ? current : current ? [current] : []
|
|
174
|
+
)
|
|
175
|
+
// The default strategy is session-only and would shadow this one.
|
|
176
|
+
.filter((strategy) => !(strategy instanceof DefaultActiveOrderStrategy));
|
|
177
|
+
if (
|
|
178
|
+
!strategies.some(
|
|
179
|
+
(strategy) => strategy instanceof JarShopMarketActiveOrderStrategy,
|
|
180
|
+
)
|
|
181
|
+
) {
|
|
182
|
+
strategies.push(new JarShopMarketActiveOrderStrategy());
|
|
183
|
+
}
|
|
184
|
+
config.orderOptions.activeOrderStrategy = strategies;
|
|
185
|
+
return config;
|
|
186
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { parseJarShopConfig } from "../../jarshop-config.js";
|
|
4
|
+
import {
|
|
5
|
+
marketsWithoutChannel,
|
|
6
|
+
reportMarketChannels,
|
|
7
|
+
} from "./market-channels.js";
|
|
8
|
+
|
|
9
|
+
const market = (code: string, currency: string, country: string) => ({
|
|
10
|
+
code,
|
|
11
|
+
channelToken: `${code}-token`,
|
|
12
|
+
defaultLanguage: "en",
|
|
13
|
+
languages: ["en"],
|
|
14
|
+
currency,
|
|
15
|
+
countries: [country],
|
|
16
|
+
domains: [`shop.example.${code}`],
|
|
17
|
+
});
|
|
18
|
+
const config = parseJarShopConfig({
|
|
19
|
+
defaultMarket: "sk",
|
|
20
|
+
markets: [market("sk", "EUR", "SK"), market("cz", "CZK", "CZ")],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("market Channel check", () => {
|
|
24
|
+
it("passes when every market token names a Channel", () => {
|
|
25
|
+
expect(
|
|
26
|
+
reportMarketChannels(config, ["sk-token", "cz-token", "other"], {
|
|
27
|
+
APP_ENV: "production",
|
|
28
|
+
}),
|
|
29
|
+
).toEqual({ level: "ok" });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("stops a production server that lacks a market Channel", () => {
|
|
33
|
+
expect(marketsWithoutChannel(config, ["sk-token"])).toHaveLength(1);
|
|
34
|
+
const report = reportMarketChannels(config, ["sk-token"], {
|
|
35
|
+
APP_ENV: "production",
|
|
36
|
+
});
|
|
37
|
+
expect(report).toMatchObject({ level: "error" });
|
|
38
|
+
expect(report).toHaveProperty(
|
|
39
|
+
"message",
|
|
40
|
+
expect.stringContaining('"cz" (channelToken "cz-token")'),
|
|
41
|
+
);
|
|
42
|
+
expect(
|
|
43
|
+
reportMarketChannels(config, ["sk-token"], {
|
|
44
|
+
APP_ENV: "production",
|
|
45
|
+
JARSHOP_MARKET_CHANNEL_CHECK: "warn",
|
|
46
|
+
}),
|
|
47
|
+
).toMatchObject({ level: "warn" });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("only warns in development, before the seed creates the Channels", () => {
|
|
51
|
+
expect(
|
|
52
|
+
reportMarketChannels(config, ["sk-token"], { APP_ENV: "dev" }),
|
|
53
|
+
).toMatchObject({
|
|
54
|
+
level: "warn",
|
|
55
|
+
message: expect.stringContaining("pnpm db:seed"),
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
type OnApplicationBootstrap,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import {
|
|
7
|
+
Channel,
|
|
8
|
+
Logger,
|
|
9
|
+
ProcessContext,
|
|
10
|
+
TransactionalConnection,
|
|
11
|
+
} from "@vendure/core";
|
|
12
|
+
|
|
13
|
+
import type { JarShopConfig, JarShopMarket } from "../../jarshop-config.js";
|
|
14
|
+
import { loadJarShopConfig } from "../../jarshop-config-loader.js";
|
|
15
|
+
|
|
16
|
+
const loggerContext = "JarShopMarkets";
|
|
17
|
+
|
|
18
|
+
export type MarketChannelReport =
|
|
19
|
+
| { readonly level: "ok" }
|
|
20
|
+
| { readonly level: "warn" | "error"; readonly message: string };
|
|
21
|
+
|
|
22
|
+
/** Markets whose `channelToken` names no existing Vendure Channel. */
|
|
23
|
+
export function marketsWithoutChannel(
|
|
24
|
+
config: JarShopConfig,
|
|
25
|
+
channelTokens: readonly string[],
|
|
26
|
+
): JarShopMarket[] {
|
|
27
|
+
const existing = new Set(channelTokens);
|
|
28
|
+
return config.markets.filter((market) => !existing.has(market.channelToken));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A market without its Channel would silently serve the default Channel's
|
|
33
|
+
* prices and carts. Outside development and test that stops the server;
|
|
34
|
+
* `JARSHOP_MARKET_CHANNEL_CHECK=warn` lets an operator start it once to create
|
|
35
|
+
* the Channels in the Dashboard.
|
|
36
|
+
*/
|
|
37
|
+
export function reportMarketChannels(
|
|
38
|
+
config: JarShopConfig,
|
|
39
|
+
channelTokens: readonly string[],
|
|
40
|
+
environment: NodeJS.ProcessEnv = process.env,
|
|
41
|
+
): MarketChannelReport {
|
|
42
|
+
const missing = marketsWithoutChannel(config, channelTokens);
|
|
43
|
+
if (missing.length === 0) return { level: "ok" };
|
|
44
|
+
const development =
|
|
45
|
+
environment.APP_ENV === "dev" || environment.APP_ENV === "test";
|
|
46
|
+
const lenient = environment.JARSHOP_MARKET_CHANNEL_CHECK === "warn";
|
|
47
|
+
const list = missing
|
|
48
|
+
.map((market) => `"${market.code}" (channelToken "${market.channelToken}")`)
|
|
49
|
+
.join(", ");
|
|
50
|
+
const remedy = development
|
|
51
|
+
? "Run `pnpm db:seed` to create them."
|
|
52
|
+
: "Create a Channel with exactly that token for each market in the Dashboard (Settings > Channels). " +
|
|
53
|
+
"To start the server once for that, set JARSHOP_MARKET_CHANNEL_CHECK=warn.";
|
|
54
|
+
return {
|
|
55
|
+
level: development || lenient ? "warn" : "error",
|
|
56
|
+
message: `jarshop.config.ts declares markets without a Vendure Channel: ${list}. ${remedy}`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Checks on server start that every configured market has its Channel. */
|
|
61
|
+
@Injectable()
|
|
62
|
+
export class JarShopMarketChannelCheck implements OnApplicationBootstrap {
|
|
63
|
+
constructor(
|
|
64
|
+
@Inject(TransactionalConnection)
|
|
65
|
+
private readonly connection: TransactionalConnection,
|
|
66
|
+
@Inject(ProcessContext)
|
|
67
|
+
private readonly processContext: ProcessContext,
|
|
68
|
+
) {}
|
|
69
|
+
|
|
70
|
+
async onApplicationBootstrap(): Promise<void> {
|
|
71
|
+
if (!this.processContext.isServer) return;
|
|
72
|
+
const channels = await this.connection.rawConnection
|
|
73
|
+
.getRepository(Channel)
|
|
74
|
+
.find({ select: { token: true } });
|
|
75
|
+
const report = reportMarketChannels(
|
|
76
|
+
loadJarShopConfig(),
|
|
77
|
+
channels.map((channel) => channel.token),
|
|
78
|
+
);
|
|
79
|
+
if (report.level === "ok") return;
|
|
80
|
+
if (report.level === "warn") {
|
|
81
|
+
Logger.warn(report.message, loggerContext);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
Logger.error(report.message, loggerContext);
|
|
85
|
+
throw new Error(report.message);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
type OnApplicationBootstrap,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import { ConfigService } from "@vendure/core";
|
|
7
|
+
|
|
8
|
+
import { assertUniqueOperationCodes } from "../../operation-codes.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Plugins in `features.plugins`, and Feature plugins such as the redirect
|
|
12
|
+
* payment plugin, may add payment handlers or shipping operations in their own
|
|
13
|
+
* `configuration` hook, after `createVendureConfig` has checked the client's
|
|
14
|
+
* lists. This check reads Vendure's final configuration on startup, so a code
|
|
15
|
+
* that a plugin registers twice, or that clashes with a client operation,
|
|
16
|
+
* stops the server instead of silently shadowing the other one.
|
|
17
|
+
*/
|
|
18
|
+
@Injectable()
|
|
19
|
+
export class JarShopOperationCodeCheck implements OnApplicationBootstrap {
|
|
20
|
+
constructor(
|
|
21
|
+
@Inject(ConfigService) private readonly configService: ConfigService,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
onApplicationBootstrap(): void {
|
|
25
|
+
const { paymentOptions, shippingOptions } = this.configService;
|
|
26
|
+
assertUniqueOperationCodes(
|
|
27
|
+
{
|
|
28
|
+
"Payment method handler": paymentOptions.paymentMethodHandlers,
|
|
29
|
+
"Shipping eligibility checker":
|
|
30
|
+
shippingOptions.shippingEligibilityCheckers ?? [],
|
|
31
|
+
"Shipping calculator": shippingOptions.shippingCalculators ?? [],
|
|
32
|
+
},
|
|
33
|
+
"Duplicate operation codes in the Vendure configuration, including operations added by plugins",
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import type { FulfillmentLifecycleEmailKind } from "./order-email-outbox.js";
|
|
10
10
|
|
|
11
11
|
export type ConfigurableOrderEmailKind =
|
|
12
|
-
FulfillmentLifecycleEmailKind | "order-cancelled";
|
|
12
|
+
FulfillmentLifecycleEmailKind | "order-cancelled" | "invoice-issued";
|
|
13
13
|
|
|
14
14
|
export type OrderEmailShippingMethodConfiguration = {
|
|
15
15
|
code: string;
|