@jarwizz/create-jarshop 0.1.8 → 0.3.1
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 +26 -0
- package/dist/presets.js +94 -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 +31 -0
- package/dist/template/package.json +5 -0
- package/dist/template/pnpm-lock.yaml +3291 -1809
- 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 +335 -0
- package/dist/template/src/jarshop-config.ts +439 -0
- package/dist/template/src/jarshop-features.test.ts +222 -0
- package/dist/template/src/jarshop-features.ts +112 -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/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 +161 -57
- 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 +427 -184
- 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/vite.config.mts +13 -2
- 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,34 @@
|
|
|
1
|
+
import type { JarShopMarket } from "./jarshop-config.js";
|
|
2
|
+
import { resolveMarketLanguage } from "./jarshop-config.js";
|
|
3
|
+
import { storefrontOriginForMarket } from "./jarshop-config-loader.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Provider Features receive these values from server configuration. They stay
|
|
7
|
+
* deliberately identical: a return address may identify the storefront page,
|
|
8
|
+
* but it never decides whether money moved.
|
|
9
|
+
*/
|
|
10
|
+
export function configuredPaymentReturnUrls(
|
|
11
|
+
storefrontUrl: URL,
|
|
12
|
+
locale: string,
|
|
13
|
+
): Readonly<{ paid: string; cancelled: string; pending: string }> {
|
|
14
|
+
const page = new URL(
|
|
15
|
+
`/${locale}/checkout/payment-return`,
|
|
16
|
+
storefrontUrl,
|
|
17
|
+
).toString();
|
|
18
|
+
return { paid: page, cancelled: page, pending: page };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The return page of one market: its own domain and a language it serves, so
|
|
23
|
+
* a shopper who paid on the Czech domain comes back to the Czech domain.
|
|
24
|
+
*/
|
|
25
|
+
export function marketPaymentReturnUrls(
|
|
26
|
+
market: JarShopMarket,
|
|
27
|
+
language: string,
|
|
28
|
+
environment: NodeJS.ProcessEnv = process.env,
|
|
29
|
+
): Readonly<{ paid: string; cancelled: string; pending: string }> {
|
|
30
|
+
return configuredPaymentReturnUrls(
|
|
31
|
+
storefrontOriginForMarket(market, environment),
|
|
32
|
+
resolveMarketLanguage(market, language),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
canReconcilePaymentWithConfirmationCredential,
|
|
4
|
+
toOrderConfirmation,
|
|
5
|
+
} from "./confirmation.js";
|
|
6
|
+
|
|
7
|
+
describe("JarShop order confirmation view", () => {
|
|
8
|
+
it("refuses a payment reconciliation without the credential for that order", () => {
|
|
9
|
+
expect(
|
|
10
|
+
canReconcilePaymentWithConfirmationCredential({
|
|
11
|
+
credentialPresent: false,
|
|
12
|
+
credentialOrderCode: "ORDER-42",
|
|
13
|
+
requestedOrderCode: "ORDER-42",
|
|
14
|
+
}),
|
|
15
|
+
).toBe(false);
|
|
16
|
+
expect(
|
|
17
|
+
canReconcilePaymentWithConfirmationCredential({
|
|
18
|
+
credentialPresent: true,
|
|
19
|
+
credentialOrderCode: "ORDER-42",
|
|
20
|
+
requestedOrderCode: "ORDER-99",
|
|
21
|
+
}),
|
|
22
|
+
).toBe(false);
|
|
23
|
+
expect(
|
|
24
|
+
canReconcilePaymentWithConfirmationCredential({
|
|
25
|
+
credentialPresent: true,
|
|
26
|
+
credentialOrderCode: "ORDER-42",
|
|
27
|
+
requestedOrderCode: "ORDER-42",
|
|
28
|
+
}),
|
|
29
|
+
).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("projects only the durable customer-visible shipping address", () => {
|
|
33
|
+
expect(
|
|
34
|
+
toOrderConfirmation({
|
|
35
|
+
id: 42,
|
|
36
|
+
code: "ORDER-42",
|
|
37
|
+
state: "PaymentAuthorized",
|
|
38
|
+
totalWithTax: 2_590,
|
|
39
|
+
currencyCode: "EUR",
|
|
40
|
+
shippingAddress: {
|
|
41
|
+
fullName: "Z-BOX, Hlavná 1, Bratislava",
|
|
42
|
+
streetLine1: "Hlavná 1",
|
|
43
|
+
streetLine2: "811 01 Bratislava",
|
|
44
|
+
postalCode: "811 01",
|
|
45
|
+
city: "Bratislava",
|
|
46
|
+
countryCode: "SK",
|
|
47
|
+
phoneNumber: "+421900000000",
|
|
48
|
+
},
|
|
49
|
+
}),
|
|
50
|
+
).toEqual({
|
|
51
|
+
orderId: 42,
|
|
52
|
+
orderCode: "ORDER-42",
|
|
53
|
+
state: "PaymentAuthorized",
|
|
54
|
+
totalWithTax: 2_590,
|
|
55
|
+
currencyCode: "EUR",
|
|
56
|
+
shippingAddress: {
|
|
57
|
+
fullName: "Z-BOX, Hlavná 1, Bratislava",
|
|
58
|
+
streetLine1: "Hlavná 1",
|
|
59
|
+
streetLine2: "811 01 Bratislava",
|
|
60
|
+
postalCode: "811 01",
|
|
61
|
+
city: "Bratislava",
|
|
62
|
+
countryCode: "SK",
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("keeps a missing shipping address explicit", () => {
|
|
68
|
+
expect(
|
|
69
|
+
toOrderConfirmation({
|
|
70
|
+
id: "42",
|
|
71
|
+
code: "ORDER-42",
|
|
72
|
+
state: "PaymentAuthorized",
|
|
73
|
+
totalWithTax: 2_590,
|
|
74
|
+
currencyCode: "EUR",
|
|
75
|
+
shippingAddress: undefined,
|
|
76
|
+
}).shippingAddress,
|
|
77
|
+
).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -3,6 +3,42 @@ import { createHash, randomBytes } from "node:crypto";
|
|
|
3
3
|
export const CONFIRMATION_TTL_MS = 2 * 60 * 60 * 1000;
|
|
4
4
|
export const CONFIRMATION_COOKIE_NAME = "jarshop-confirmation";
|
|
5
5
|
|
|
6
|
+
type OrderConfirmationSource = {
|
|
7
|
+
id: string | number;
|
|
8
|
+
code: string;
|
|
9
|
+
state: string;
|
|
10
|
+
totalWithTax: number;
|
|
11
|
+
currencyCode: string;
|
|
12
|
+
shippingAddress?: {
|
|
13
|
+
fullName?: string;
|
|
14
|
+
streetLine1?: string;
|
|
15
|
+
streetLine2?: string;
|
|
16
|
+
postalCode?: string;
|
|
17
|
+
city?: string;
|
|
18
|
+
countryCode?: string;
|
|
19
|
+
} | null;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function toOrderConfirmation(order: OrderConfirmationSource) {
|
|
23
|
+
return {
|
|
24
|
+
orderId: order.id,
|
|
25
|
+
orderCode: order.code,
|
|
26
|
+
state: order.state,
|
|
27
|
+
totalWithTax: order.totalWithTax,
|
|
28
|
+
currencyCode: order.currencyCode,
|
|
29
|
+
shippingAddress: order.shippingAddress
|
|
30
|
+
? {
|
|
31
|
+
fullName: order.shippingAddress.fullName ?? "",
|
|
32
|
+
streetLine1: order.shippingAddress.streetLine1 ?? "",
|
|
33
|
+
streetLine2: order.shippingAddress.streetLine2 ?? "",
|
|
34
|
+
postalCode: order.shippingAddress.postalCode ?? "",
|
|
35
|
+
city: order.shippingAddress.city ?? "",
|
|
36
|
+
countryCode: order.shippingAddress.countryCode ?? "",
|
|
37
|
+
}
|
|
38
|
+
: null,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
6
42
|
export function hashConfirmationToken(token: string): string {
|
|
7
43
|
return createHash("sha256").update(token).digest("hex");
|
|
8
44
|
}
|
|
@@ -51,6 +87,21 @@ export function canReadConfirmationCredential(input: {
|
|
|
51
87
|
);
|
|
52
88
|
}
|
|
53
89
|
|
|
90
|
+
/**
|
|
91
|
+
* A confirmation credential can inspect or reconcile only the one order it
|
|
92
|
+
* was exchanged for. The return URL is merely a hint naming that order.
|
|
93
|
+
*/
|
|
94
|
+
export function canReconcilePaymentWithConfirmationCredential(input: {
|
|
95
|
+
credentialPresent: boolean;
|
|
96
|
+
credentialOrderCode: string | undefined;
|
|
97
|
+
requestedOrderCode: string;
|
|
98
|
+
}): boolean {
|
|
99
|
+
return (
|
|
100
|
+
input.credentialPresent &&
|
|
101
|
+
input.credentialOrderCode === input.requestedOrderCode
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
54
105
|
export function readCookieValue(
|
|
55
106
|
cookieHeader: string,
|
|
56
107
|
cookieName: string,
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
2
|
+
import { ModuleRef } from "@nestjs/core";
|
|
3
|
+
import { Args, Mutation, Resolver } from "@nestjs/graphql";
|
|
4
|
+
import {
|
|
5
|
+
Allow,
|
|
6
|
+
Ctx,
|
|
7
|
+
ForbiddenError,
|
|
8
|
+
Injector,
|
|
9
|
+
Permission,
|
|
10
|
+
PluginCommonModule,
|
|
11
|
+
RequestContext,
|
|
12
|
+
VendurePlugin,
|
|
13
|
+
} from "@vendure/core";
|
|
14
|
+
import { parse } from "graphql";
|
|
15
|
+
import {
|
|
16
|
+
getJarShopInvoiceDeliveryEvent,
|
|
17
|
+
jarShopInvoicePermission,
|
|
18
|
+
} from "@jarwizz/jarshop-invoicing/server";
|
|
19
|
+
import { JarShopInvoiceEmailOutbox } from "./invoice-email-lifecycle.js";
|
|
20
|
+
|
|
21
|
+
const adminSchema = parse(`
|
|
22
|
+
extend type Mutation {
|
|
23
|
+
jarShopRedeliverInvoice(invoiceId: ID!, requestId: String!): Boolean!
|
|
24
|
+
}
|
|
25
|
+
`);
|
|
26
|
+
|
|
27
|
+
@Injectable()
|
|
28
|
+
@Resolver()
|
|
29
|
+
class JarShopInvoiceRedeliveryResolver {
|
|
30
|
+
constructor(
|
|
31
|
+
@Inject(JarShopInvoiceEmailOutbox)
|
|
32
|
+
private readonly invoiceOutbox: JarShopInvoiceEmailOutbox,
|
|
33
|
+
@Inject(ModuleRef)
|
|
34
|
+
private readonly moduleRef: ModuleRef,
|
|
35
|
+
) {}
|
|
36
|
+
|
|
37
|
+
@Mutation()
|
|
38
|
+
@Allow(jarShopInvoicePermission.Permission, Permission.UpdateOrder)
|
|
39
|
+
async jarShopRedeliverInvoice(
|
|
40
|
+
@Ctx() ctx: RequestContext,
|
|
41
|
+
@Args("invoiceId") invoiceId: string,
|
|
42
|
+
@Args("requestId") requestId: string,
|
|
43
|
+
) {
|
|
44
|
+
// Vendure's @Allow arguments are ORed; resending requires both grants.
|
|
45
|
+
if (
|
|
46
|
+
!ctx.userHasAllPermissions([
|
|
47
|
+
jarShopInvoicePermission.Permission,
|
|
48
|
+
Permission.UpdateOrder,
|
|
49
|
+
])
|
|
50
|
+
) {
|
|
51
|
+
throw new ForbiddenError();
|
|
52
|
+
}
|
|
53
|
+
const event = await getJarShopInvoiceDeliveryEvent(
|
|
54
|
+
ctx,
|
|
55
|
+
new Injector(this.moduleRef),
|
|
56
|
+
invoiceId,
|
|
57
|
+
);
|
|
58
|
+
await this.invoiceOutbox.redeliver(event, requestId);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Invoice email delivery through the order-email outbox.
|
|
65
|
+
*
|
|
66
|
+
* It is registered only beside the invoicing Feature, so a Client Project
|
|
67
|
+
* that switches invoicing off has no invoice outbox, no reconciliation scan
|
|
68
|
+
* and no redelivery mutation. The order-email worker then fails a leftover
|
|
69
|
+
* invoice delivery with `INVOICING_DISABLED` instead of reading a PDF.
|
|
70
|
+
*/
|
|
71
|
+
@VendurePlugin({
|
|
72
|
+
imports: [PluginCommonModule],
|
|
73
|
+
providers: [JarShopInvoiceEmailOutbox],
|
|
74
|
+
adminApiExtensions: {
|
|
75
|
+
schema: adminSchema,
|
|
76
|
+
resolvers: [JarShopInvoiceRedeliveryResolver],
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
export class JarShopInvoiceDeliveryPlugin {}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { JarShopInvoiceCreatedEvent } from "@jarwizz/jarshop-invoicing/server";
|
|
2
|
+
import { createPendingInvoiceEmailDelivery } from "./order-email-outbox.js";
|
|
3
|
+
|
|
4
|
+
/** A re-creation event contains a credit note and the replacement invoice. */
|
|
5
|
+
export function createInvoiceEventDeliveries(
|
|
6
|
+
event: JarShopInvoiceCreatedEvent,
|
|
7
|
+
languageCode: string = event.ctx.languageCode,
|
|
8
|
+
) {
|
|
9
|
+
if (!event.order.customer?.emailAddress)
|
|
10
|
+
throw new Error("INVOICE_RECIPIENT_MISSING");
|
|
11
|
+
const invoices = [event.creditInvoice, event.newInvoice].filter(
|
|
12
|
+
(invoice): invoice is JarShopInvoiceCreatedEvent["newInvoice"] => !!invoice,
|
|
13
|
+
);
|
|
14
|
+
return [
|
|
15
|
+
...new Map(
|
|
16
|
+
invoices.map((invoice) => [String(invoice.id), invoice]),
|
|
17
|
+
).values(),
|
|
18
|
+
].map((invoice) => {
|
|
19
|
+
if (
|
|
20
|
+
invoice.channelId !== String(event.ctx.channelId) ||
|
|
21
|
+
invoice.orderId !== String(event.order.id)
|
|
22
|
+
) {
|
|
23
|
+
throw new Error("INVOICE_NOT_ACCESSIBLE");
|
|
24
|
+
}
|
|
25
|
+
return createPendingInvoiceEmailDelivery({
|
|
26
|
+
orderId: Number(event.order.id),
|
|
27
|
+
snapshot: {
|
|
28
|
+
invoiceId: String(invoice.id),
|
|
29
|
+
channelId: String(event.ctx.channelId),
|
|
30
|
+
recipient: event.order.customer!.emailAddress,
|
|
31
|
+
languageCode,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
Logger,
|
|
5
|
+
type OnApplicationBootstrap,
|
|
6
|
+
type OnModuleDestroy,
|
|
7
|
+
} from "@nestjs/common";
|
|
8
|
+
import {
|
|
9
|
+
Channel,
|
|
10
|
+
EventBus,
|
|
11
|
+
Injector,
|
|
12
|
+
RequestContextService,
|
|
13
|
+
TransactionalConnection,
|
|
14
|
+
} from "@vendure/core";
|
|
15
|
+
import { ModuleRef } from "@nestjs/core";
|
|
16
|
+
import { In } from "typeorm";
|
|
17
|
+
import {
|
|
18
|
+
JarShopInvoiceCreatedEvent,
|
|
19
|
+
getJarShopInvoiceDeliveryPage,
|
|
20
|
+
getJarShopInvoiceDeliveryEvents,
|
|
21
|
+
} from "@jarwizz/jarshop-invoicing/server";
|
|
22
|
+
import { JarShopEmailBrandConfiguration } from "./order-email-configuration.entity.js";
|
|
23
|
+
import { isLifecycleEmailEnabled } from "./order-email-configuration.js";
|
|
24
|
+
import { JarShopOrderEmailDelivery } from "./order-email-delivery.entity.js";
|
|
25
|
+
import { createInvoiceEventDeliveries } from "./invoice-email-events.js";
|
|
26
|
+
import {
|
|
27
|
+
createPendingInvoiceEmailRedelivery,
|
|
28
|
+
decryptOrderEmailOutboxPayload,
|
|
29
|
+
isSnapshotOrderEmailOutboxPayload,
|
|
30
|
+
} from "./order-email-outbox.js";
|
|
31
|
+
|
|
32
|
+
@Injectable()
|
|
33
|
+
export class JarShopInvoiceEmailOutbox
|
|
34
|
+
implements OnApplicationBootstrap, OnModuleDestroy
|
|
35
|
+
{
|
|
36
|
+
private subscription?: { unsubscribe(): void };
|
|
37
|
+
private readonly scanCursors = new Map<string, string>();
|
|
38
|
+
|
|
39
|
+
constructor(
|
|
40
|
+
@Inject(TransactionalConnection)
|
|
41
|
+
private readonly connection: TransactionalConnection,
|
|
42
|
+
@Inject(EventBus) private readonly eventBus: EventBus,
|
|
43
|
+
@Inject(RequestContextService)
|
|
44
|
+
private readonly contextService: RequestContextService,
|
|
45
|
+
@Inject(ModuleRef) private readonly moduleRef: ModuleRef,
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
onApplicationBootstrap(): void {
|
|
49
|
+
// A delivery outage must never make the issuing job re-create an invoice.
|
|
50
|
+
this.subscription = this.eventBus
|
|
51
|
+
.ofType(JarShopInvoiceCreatedEvent)
|
|
52
|
+
.subscribe((event) => {
|
|
53
|
+
void this.enqueue(event).catch(() =>
|
|
54
|
+
Logger.error(
|
|
55
|
+
"INVOICE_EMAIL_ENQUEUE_FAILED",
|
|
56
|
+
"JarShopInvoiceEmailOutbox",
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
onModuleDestroy(): void {
|
|
63
|
+
this.subscription?.unsubscribe();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Scan a bounded page per enabled Channel. Restarting only repeats safe reads. */
|
|
67
|
+
async reconcile(): Promise<void> {
|
|
68
|
+
const configurations = await this.connection.rawConnection
|
|
69
|
+
.getRepository(JarShopEmailBrandConfiguration)
|
|
70
|
+
.find();
|
|
71
|
+
const injector = new Injector(this.moduleRef);
|
|
72
|
+
for (const configuration of configurations) {
|
|
73
|
+
if (!isLifecycleEmailEnabled(configuration, "invoice-issued")) continue;
|
|
74
|
+
const channelId = String(configuration.channelId);
|
|
75
|
+
const channel = await this.connection.rawConnection
|
|
76
|
+
.getRepository(Channel)
|
|
77
|
+
.findOne({ where: { id: channelId } });
|
|
78
|
+
if (!channel) continue;
|
|
79
|
+
const ctx = await this.contextService.create({
|
|
80
|
+
apiType: "admin",
|
|
81
|
+
channelOrToken: channel.token,
|
|
82
|
+
});
|
|
83
|
+
const page = await getJarShopInvoiceDeliveryPage(
|
|
84
|
+
ctx,
|
|
85
|
+
injector,
|
|
86
|
+
this.scanCursors.get(channelId) ?? "0",
|
|
87
|
+
);
|
|
88
|
+
if (!page.length) {
|
|
89
|
+
this.scanCursors.delete(channelId);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const existing = await this.connection
|
|
93
|
+
.getRepository(ctx, JarShopOrderEmailDelivery)
|
|
94
|
+
.find({
|
|
95
|
+
where: {
|
|
96
|
+
eventKey: In(
|
|
97
|
+
page.map(({ invoiceId }) => `invoice:${invoiceId}:issued`),
|
|
98
|
+
),
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
const keys = new Set(existing.map((delivery) => delivery.eventKey));
|
|
102
|
+
const missingOrders = new Set(
|
|
103
|
+
page
|
|
104
|
+
.filter(({ invoiceId }) => !keys.has(`invoice:${invoiceId}:issued`))
|
|
105
|
+
.map(({ orderId }) => orderId),
|
|
106
|
+
);
|
|
107
|
+
for (const orderId of missingOrders) {
|
|
108
|
+
try {
|
|
109
|
+
for (const event of await getJarShopInvoiceDeliveryEvents(
|
|
110
|
+
ctx,
|
|
111
|
+
injector,
|
|
112
|
+
orderId,
|
|
113
|
+
))
|
|
114
|
+
await this.enqueue(event);
|
|
115
|
+
} catch {
|
|
116
|
+
// Keep scanning other orders. A later full pass retries this one.
|
|
117
|
+
Logger.error(
|
|
118
|
+
"INVOICE_EMAIL_RECONCILIATION_FAILED",
|
|
119
|
+
"JarShopInvoiceEmailOutbox",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this.scanCursors.set(channelId, page[page.length - 1]!.invoiceId);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async enqueue(event: JarShopInvoiceCreatedEvent): Promise<boolean> {
|
|
128
|
+
const configuration = await this.connection
|
|
129
|
+
.getRepository(event.ctx, JarShopEmailBrandConfiguration)
|
|
130
|
+
.findOne({ where: { channelId: Number(event.ctx.channelId) } });
|
|
131
|
+
if (!isLifecycleEmailEnabled(configuration, "invoice-issued")) return false;
|
|
132
|
+
const repository = this.connection.getRepository(
|
|
133
|
+
event.ctx,
|
|
134
|
+
JarShopOrderEmailDelivery,
|
|
135
|
+
);
|
|
136
|
+
const languageCode = await this.resolveCustomerLanguage(event);
|
|
137
|
+
const deliveries = createInvoiceEventDeliveries(event, languageCode);
|
|
138
|
+
await repository
|
|
139
|
+
.createQueryBuilder()
|
|
140
|
+
.insert()
|
|
141
|
+
.values(deliveries)
|
|
142
|
+
.orIgnore()
|
|
143
|
+
.execute();
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async recover(event: JarShopInvoiceCreatedEvent): Promise<void> {
|
|
148
|
+
if (!(await this.enqueue(event))) throw new Error("EMAIL_KIND_NOT_ENABLED");
|
|
149
|
+
for (const delivery of createInvoiceEventDeliveries(event)) {
|
|
150
|
+
await this.connection
|
|
151
|
+
.getRepository(event.ctx, JarShopOrderEmailDelivery)
|
|
152
|
+
.update(
|
|
153
|
+
{
|
|
154
|
+
eventKey: delivery.eventKey,
|
|
155
|
+
orderId: Number(event.order.id),
|
|
156
|
+
kind: "invoice-issued",
|
|
157
|
+
status: "failed",
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
status: "pending",
|
|
161
|
+
attemptCount: 0,
|
|
162
|
+
nextAttemptAt: new Date(),
|
|
163
|
+
lastErrorCode: null,
|
|
164
|
+
leaseToken: null,
|
|
165
|
+
leaseExpiresAt: null,
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** An explicit operator resend creates a new intent but never a new invoice. */
|
|
172
|
+
async redeliver(
|
|
173
|
+
event: JarShopInvoiceCreatedEvent,
|
|
174
|
+
requestId: string,
|
|
175
|
+
): Promise<void> {
|
|
176
|
+
const configuration = await this.connection
|
|
177
|
+
.getRepository(event.ctx, JarShopEmailBrandConfiguration)
|
|
178
|
+
.findOne({ where: { channelId: Number(event.ctx.channelId) } });
|
|
179
|
+
if (!isLifecycleEmailEnabled(configuration, "invoice-issued")) {
|
|
180
|
+
throw new Error("EMAIL_KIND_NOT_ENABLED");
|
|
181
|
+
}
|
|
182
|
+
const languageCode = await this.resolveCustomerLanguage(event);
|
|
183
|
+
const deliveries = createInvoiceEventDeliveries(event, languageCode).map(
|
|
184
|
+
(delivery) => {
|
|
185
|
+
const payload = decryptOrderEmailOutboxPayload(
|
|
186
|
+
delivery.payloadCiphertext,
|
|
187
|
+
);
|
|
188
|
+
if (!("kind" in payload) || payload.kind !== "invoice-issued") {
|
|
189
|
+
throw new Error("INVOICE_DELIVERY_PAYLOAD_INVALID");
|
|
190
|
+
}
|
|
191
|
+
return createPendingInvoiceEmailRedelivery({
|
|
192
|
+
orderId: delivery.orderId,
|
|
193
|
+
snapshot: payload.snapshot,
|
|
194
|
+
requestId,
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
);
|
|
198
|
+
await this.connection
|
|
199
|
+
.getRepository(event.ctx, JarShopOrderEmailDelivery)
|
|
200
|
+
.createQueryBuilder()
|
|
201
|
+
.insert()
|
|
202
|
+
.values(deliveries)
|
|
203
|
+
.orIgnore()
|
|
204
|
+
.execute();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private async resolveCustomerLanguage(event: JarShopInvoiceCreatedEvent) {
|
|
208
|
+
const confirmation = await this.connection
|
|
209
|
+
.getRepository(event.ctx, JarShopOrderEmailDelivery)
|
|
210
|
+
.findOne({
|
|
211
|
+
where: { orderId: Number(event.order.id), kind: "order-confirmation" },
|
|
212
|
+
});
|
|
213
|
+
const payload = confirmation
|
|
214
|
+
? decryptOrderEmailOutboxPayload(confirmation.payloadCiphertext)
|
|
215
|
+
: undefined;
|
|
216
|
+
// Confirmation owns the customer's checkout language, not the Admin context.
|
|
217
|
+
return payload && isSnapshotOrderEmailOutboxPayload(payload)
|
|
218
|
+
? payload.snapshot.languageCode
|
|
219
|
+
: event.ctx.languageCode;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The invoice outbox, or `undefined` when the Client Project switched the
|
|
225
|
+
* invoicing Feature off and `JarShopInvoiceDeliveryPlugin` is not registered.
|
|
226
|
+
*/
|
|
227
|
+
export function findJarShopInvoiceEmailOutbox(
|
|
228
|
+
injector: Pick<Injector, "get">,
|
|
229
|
+
): JarShopInvoiceEmailOutbox | undefined {
|
|
230
|
+
try {
|
|
231
|
+
return injector.get(JarShopInvoiceEmailOutbox);
|
|
232
|
+
} catch {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
}
|