@porulle/plugin-giftcards 0.11.0 → 0.13.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/dist/hooks/checkout-deduction.d.ts.map +1 -1
- package/dist/hooks/checkout-deduction.js +6 -4
- package/dist/hooks/checkout-issuance.d.ts.map +1 -1
- package/dist/hooks/checkout-issuance.js +2 -2
- package/dist/hooks/refund-credit.d.ts.map +1 -1
- package/dist/hooks/refund-credit.js +2 -2
- package/dist/routes/customer.js +2 -2
- package/package.json +4 -4
- package/src/hooks/checkout-deduction.ts +9 -5
- package/src/hooks/checkout-issuance.ts +4 -3
- package/src/hooks/refund-credit.ts +4 -3
- package/src/routes/customer.ts +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkout-deduction.d.ts","sourceRoot":"","sources":["../../src/hooks/checkout-deduction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"checkout-deduction.d.ts","sourceRoot":"","sources":["../../src/hooks/checkout-deduction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkB,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAyBxE;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,eAAe,GACvB,sBAAsB,CAkDxB;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,EAAE,eAAe,GACvB,sBAAsB,CA4BxB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveOrgIdForCommerce } from "@porulle/core";
|
|
2
2
|
/**
|
|
3
3
|
* checkout.beforePayment hook — deducts gift card balances before the
|
|
4
4
|
* payment adapter authorizes the remaining amount.
|
|
@@ -6,10 +6,10 @@ import { resolveOrgId } from "@porulle/core";
|
|
|
6
6
|
export function buildCheckoutDeductionHook(service) {
|
|
7
7
|
const handler = async (args) => {
|
|
8
8
|
const { data, context } = args;
|
|
9
|
-
const orgId = resolveOrgId(context.actor);
|
|
10
9
|
const codes = data.metadata?.giftCardCodes;
|
|
11
10
|
if (!codes?.length)
|
|
12
11
|
return data;
|
|
12
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
13
13
|
let remaining = data.total;
|
|
14
14
|
const deductions = [];
|
|
15
15
|
for (const code of codes) {
|
|
@@ -49,10 +49,12 @@ export function buildCheckoutDeductionHook(service) {
|
|
|
49
49
|
export function buildCheckoutCompensationHook(service) {
|
|
50
50
|
const handler = async (args) => {
|
|
51
51
|
const { data, result, context } = args;
|
|
52
|
-
const orgId = resolveOrgId(context.actor);
|
|
53
52
|
if (!result) {
|
|
54
53
|
const deductions = data.metadata?.giftCardDeductions;
|
|
55
|
-
|
|
54
|
+
if (!deductions?.length)
|
|
55
|
+
return;
|
|
56
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
57
|
+
for (const d of deductions) {
|
|
56
58
|
await service.creditWithLock(orgId, d.code, d.amount, data.checkoutId, "Checkout failed — balance restored");
|
|
57
59
|
}
|
|
58
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkout-issuance.d.ts","sourceRoot":"","sources":["../../src/hooks/checkout-issuance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"checkout-issuance.d.ts","sourceRoot":"","sources":["../../src/hooks/checkout-issuance.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkB,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACxE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AA8BzD;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,eAAe,EACxB,OAAO,EAAE,QAAQ,CAAC,qBAAqB,CAAC,EACxC,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,GAC7E,sBAAsB,CAqDxB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveOrgIdForCommerce } from "@porulle/core";
|
|
2
2
|
/**
|
|
3
3
|
* checkout.afterCreate hook — issues gift cards when a gift card product is purchased.
|
|
4
4
|
*/
|
|
@@ -7,7 +7,7 @@ export function buildGiftCardIssuanceHook(service, options, enqueueJob) {
|
|
|
7
7
|
const { result, context } = args;
|
|
8
8
|
if (!result?.lineItems)
|
|
9
9
|
return;
|
|
10
|
-
const orgId =
|
|
10
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
11
11
|
for (const item of result.lineItems) {
|
|
12
12
|
if (item.entityType !== options.productType)
|
|
13
13
|
continue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refund-credit.d.ts","sourceRoot":"","sources":["../../src/hooks/refund-credit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"refund-credit.d.ts","sourceRoot":"","sources":["../../src/hooks/refund-credit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkB,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAmBxE;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,eAAe,GACvB,sBAAsB,CA8BxB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveOrgIdForCommerce } from "@porulle/core";
|
|
2
2
|
/**
|
|
3
3
|
* order.afterUpdate hook — restores gift card balances when an order is refunded.
|
|
4
4
|
*/
|
|
@@ -13,7 +13,7 @@ export function buildRefundCreditHook(service) {
|
|
|
13
13
|
const deductions = result.metadata?.giftCardDeductions;
|
|
14
14
|
if (!deductions?.length)
|
|
15
15
|
return;
|
|
16
|
-
const orgId =
|
|
16
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
17
17
|
for (const d of deductions) {
|
|
18
18
|
await service.creditWithLock(orgId, d.code, d.amount, result.id, `Order ${status} — balance restored`);
|
|
19
19
|
}
|
package/dist/routes/customer.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { router } from "@porulle/core";
|
|
1
|
+
import { requireUserId, router } from "@porulle/core";
|
|
2
2
|
import { formatCode } from "../code-generator.js";
|
|
3
3
|
export function buildCustomerRoutes(service, ctx) {
|
|
4
4
|
const r = router("Gift Cards (Customer)", "/me/gift-cards", ctx);
|
|
@@ -9,7 +9,7 @@ export function buildCustomerRoutes(service, ctx) {
|
|
|
9
9
|
.handler(async ({ actor, orgId }) => {
|
|
10
10
|
if (!actor)
|
|
11
11
|
throw new Error("Unauthorized");
|
|
12
|
-
const result = await service.list(orgId, { purchaserId: actor
|
|
12
|
+
const result = await service.list(orgId, { purchaserId: requireUserId(actor) });
|
|
13
13
|
if (!result.ok)
|
|
14
14
|
throw new Error("Failed to list gift cards");
|
|
15
15
|
return result.value.map((card) => ({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@porulle/plugin-giftcards",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -19,15 +19,15 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@hono/zod-openapi": "^1.2.2",
|
|
21
21
|
"hono": "^4.12.5",
|
|
22
|
-
"@porulle/core": "0.
|
|
22
|
+
"@porulle/core": "0.13.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^24.5.2",
|
|
26
26
|
"eslint": "^9.39.1",
|
|
27
27
|
"typescript": "5.9.2",
|
|
28
28
|
"vitest": "^3.2.4",
|
|
29
|
-
"@porulle/
|
|
30
|
-
"@porulle/
|
|
29
|
+
"@porulle/typescript-config": "0.1.0",
|
|
30
|
+
"@porulle/eslint-config": "0.1.0"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { PluginHookRegistration } from "@porulle/core";
|
|
1
|
+
import { resolveOrgIdForCommerce } from "@porulle/core";
|
|
2
|
+
import type { CommerceConfig, PluginHookRegistration } from "@porulle/core";
|
|
3
3
|
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
4
|
import type { GiftCardDeduction } from "../types.js";
|
|
5
5
|
|
|
6
6
|
interface HookContextLike {
|
|
7
7
|
actor: { organizationId?: string | null; [key: string]: unknown } | null;
|
|
8
|
+
commerceConfig?: CommerceConfig | null;
|
|
8
9
|
[key: string]: unknown;
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -33,9 +34,9 @@ export function buildCheckoutDeductionHook(
|
|
|
33
34
|
): PluginHookRegistration {
|
|
34
35
|
const handler = async (args: CheckoutHookArgs) => {
|
|
35
36
|
const { data, context } = args;
|
|
36
|
-
const orgId = resolveOrgId(context.actor);
|
|
37
37
|
const codes = data.metadata?.giftCardCodes as string[] | undefined;
|
|
38
38
|
if (!codes?.length) return data;
|
|
39
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
39
40
|
|
|
40
41
|
let remaining = data.total;
|
|
41
42
|
const deductions: GiftCardDeduction[] = [];
|
|
@@ -90,13 +91,16 @@ export function buildCheckoutCompensationHook(
|
|
|
90
91
|
): PluginHookRegistration {
|
|
91
92
|
const handler = async (args: AfterCreateHookArgs) => {
|
|
92
93
|
const { data, result, context } = args;
|
|
93
|
-
const orgId = resolveOrgId(context.actor);
|
|
94
94
|
if (!result) {
|
|
95
95
|
const deductions = data.metadata?.giftCardDeductions as
|
|
96
96
|
| GiftCardDeduction[]
|
|
97
97
|
| undefined;
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
if (!deductions?.length) return;
|
|
100
|
+
|
|
101
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
102
|
+
|
|
103
|
+
for (const d of deductions) {
|
|
100
104
|
await service.creditWithLock(
|
|
101
105
|
orgId,
|
|
102
106
|
d.code,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { PluginHookRegistration } from "@porulle/core";
|
|
1
|
+
import { resolveOrgIdForCommerce } from "@porulle/core";
|
|
2
|
+
import type { CommerceConfig, PluginHookRegistration } from "@porulle/core";
|
|
3
3
|
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
4
|
import type { GiftCardPluginOptions } from "../types.js";
|
|
5
5
|
|
|
@@ -21,6 +21,7 @@ interface OrderResult {
|
|
|
21
21
|
|
|
22
22
|
interface HookContextLike {
|
|
23
23
|
actor: { organizationId?: string | null; [key: string]: unknown } | null;
|
|
24
|
+
commerceConfig?: CommerceConfig | null;
|
|
24
25
|
[key: string]: unknown;
|
|
25
26
|
}
|
|
26
27
|
|
|
@@ -42,7 +43,7 @@ export function buildGiftCardIssuanceHook(
|
|
|
42
43
|
const { result, context } = args;
|
|
43
44
|
if (!result?.lineItems) return;
|
|
44
45
|
|
|
45
|
-
const orgId =
|
|
46
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
46
47
|
|
|
47
48
|
for (const item of result.lineItems) {
|
|
48
49
|
if (item.entityType !== options.productType) continue;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { PluginHookRegistration } from "@porulle/core";
|
|
1
|
+
import { resolveOrgIdForCommerce } from "@porulle/core";
|
|
2
|
+
import type { CommerceConfig, PluginHookRegistration } from "@porulle/core";
|
|
3
3
|
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
4
|
import type { GiftCardDeduction } from "../types.js";
|
|
5
5
|
|
|
6
6
|
interface HookContextLike {
|
|
7
7
|
actor: { organizationId?: string | null; [key: string]: unknown } | null;
|
|
8
|
+
commerceConfig?: CommerceConfig | null;
|
|
8
9
|
[key: string]: unknown;
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -37,7 +38,7 @@ export function buildRefundCreditHook(
|
|
|
37
38
|
|
|
38
39
|
if (!deductions?.length) return;
|
|
39
40
|
|
|
40
|
-
const orgId =
|
|
41
|
+
const orgId = resolveOrgIdForCommerce(context.actor, context.commerceConfig);
|
|
41
42
|
for (const d of deductions) {
|
|
42
43
|
await service.creditWithLock(
|
|
43
44
|
orgId,
|
package/src/routes/customer.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { router } from "@porulle/core";
|
|
1
|
+
import { requireUserId, router } from "@porulle/core";
|
|
2
2
|
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
3
3
|
import type { PluginRouteRegistration } from "@porulle/core";
|
|
4
4
|
import { formatCode } from "../code-generator.js";
|
|
@@ -16,7 +16,7 @@ export function buildCustomerRoutes(
|
|
|
16
16
|
.auth()
|
|
17
17
|
.handler(async ({ actor, orgId }) => {
|
|
18
18
|
if (!actor) throw new Error("Unauthorized");
|
|
19
|
-
const result = await service.list(orgId, { purchaserId: actor
|
|
19
|
+
const result = await service.list(orgId, { purchaserId: requireUserId(actor) });
|
|
20
20
|
if (!result.ok) throw new Error("Failed to list gift cards");
|
|
21
21
|
return result.value.map((card) => ({
|
|
22
22
|
...card,
|