@unifiedcommerce/plugin-giftcards 0.0.1 → 0.2.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/dist/hooks/checkout-deduction.d.ts +1 -1
- package/dist/hooks/checkout-deduction.d.ts.map +1 -1
- package/dist/hooks/checkout-issuance.d.ts +2 -2
- package/dist/hooks/checkout-issuance.d.ts.map +1 -1
- package/dist/hooks/refund-credit.d.ts +1 -1
- package/dist/hooks/refund-credit.d.ts.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +81 -14
- package/dist/routes/admin.d.ts +1 -1
- package/dist/routes/admin.d.ts.map +1 -1
- package/dist/routes/admin.js +1 -1
- package/dist/routes/customer.d.ts +1 -1
- package/dist/routes/customer.d.ts.map +1 -1
- package/dist/routes/customer.js +1 -1
- package/dist/routes/public.d.ts +1 -1
- package/dist/routes/public.d.ts.map +1 -1
- package/dist/services/gift-card-repository.d.ts +1 -1
- package/dist/services/gift-card-repository.d.ts.map +1 -1
- package/dist/services/gift-card-repository.js +1 -1
- package/dist/services/gift-card-service.d.ts +1 -1
- package/dist/services/gift-card-service.d.ts.map +1 -1
- package/dist/services/gift-card-service.js +2 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/package.json +15 -5
- package/src/hooks/checkout-deduction.ts +2 -2
- package/src/hooks/checkout-issuance.ts +2 -2
- package/src/hooks/refund-credit.ts +2 -2
- package/src/index.ts +93 -21
- package/src/routes/admin.ts +2 -2
- package/src/routes/customer.ts +2 -2
- package/src/routes/public.ts +1 -1
- package/src/services/gift-card-repository.ts +2 -2
- package/src/services/gift-card-service.ts +3 -3
- package/src/types.ts +2 -2
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type { PluginDb as Db } from "@unifiedcommerce/core";
|
|
2
|
-
import type { giftCards, giftCardTransactions } from "./schema";
|
|
2
|
+
import type { giftCards, giftCardTransactions } from "./schema.js";
|
|
3
3
|
export type GiftCard = typeof giftCards.$inferSelect;
|
|
4
4
|
export type GiftCardInsert = typeof giftCards.$inferInsert;
|
|
5
5
|
export type GiftCardTransaction = typeof giftCardTransactions.$inferSelect;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnE,MAAM,MAAM,QAAQ,GAAG,OAAO,SAAS,CAAC,YAAY,CAAC;AACrD,MAAM,MAAM,cAAc,GAAG,OAAO,SAAS,CAAC,YAAY,CAAC;AAC3D,MAAM,MAAM,mBAAmB,GAAG,OAAO,oBAAoB,CAAC,YAAY,CAAC;AAC3E,MAAM,MAAM,yBAAyB,GAAG,OAAO,oBAAoB,CAAC,YAAY,CAAC;AAEjF,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,UAAU,GAAG,WAAW,CAAC;AACjE,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE5D,MAAM,WAAW,qBAAqB;IACpC,0DAA0D;IAC1D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uEAAuE;IACvE,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,+EAA+E;IAC/E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,gFAAgF;IAChF,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,8CAA8C;IAC9C,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,qFAAqF;IACrF,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,qBAAqB,CAO3D,CAAC;AAEF,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;CACtB"}
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unifiedcommerce/plugin-giftcards",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"bun": "./src/index.ts",
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"types": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"./schema": {
|
|
12
|
+
"bun": "./src/schema.ts",
|
|
13
|
+
"import": "./dist/schema.js",
|
|
14
|
+
"require": "./dist/schema.js",
|
|
15
|
+
"types": "./src/schema.ts"
|
|
9
16
|
}
|
|
10
17
|
},
|
|
11
18
|
"scripts": {
|
|
@@ -32,8 +39,11 @@
|
|
|
32
39
|
"access": "public"
|
|
33
40
|
},
|
|
34
41
|
"files": [
|
|
35
|
-
"dist",
|
|
36
42
|
"src",
|
|
43
|
+
"dist",
|
|
37
44
|
"README.md"
|
|
38
|
-
]
|
|
45
|
+
],
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"zod": ">=4.0.0"
|
|
48
|
+
}
|
|
39
49
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolveOrgId } from "@unifiedcommerce/core";
|
|
2
2
|
import type { PluginHookRegistration } from "@unifiedcommerce/core";
|
|
3
|
-
import type { GiftCardService } from "../services/gift-card-service";
|
|
4
|
-
import type { GiftCardDeduction } from "../types";
|
|
3
|
+
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
|
+
import type { GiftCardDeduction } from "../types.js";
|
|
5
5
|
|
|
6
6
|
interface HookContextLike {
|
|
7
7
|
actor: { organizationId?: string | null; [key: string]: unknown } | null;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolveOrgId } from "@unifiedcommerce/core";
|
|
2
2
|
import type { PluginHookRegistration } from "@unifiedcommerce/core";
|
|
3
|
-
import type { GiftCardService } from "../services/gift-card-service";
|
|
4
|
-
import type { GiftCardPluginOptions } from "../types";
|
|
3
|
+
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
|
+
import type { GiftCardPluginOptions } from "../types.js";
|
|
5
5
|
|
|
6
6
|
interface OrderLineItem {
|
|
7
7
|
entityId: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolveOrgId } from "@unifiedcommerce/core";
|
|
2
2
|
import type { PluginHookRegistration } from "@unifiedcommerce/core";
|
|
3
|
-
import type { GiftCardService } from "../services/gift-card-service";
|
|
4
|
-
import type { GiftCardDeduction } from "../types";
|
|
3
|
+
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
|
+
import type { GiftCardDeduction } from "../types.js";
|
|
5
5
|
|
|
6
6
|
interface HookContextLike {
|
|
7
7
|
actor: { organizationId?: string | null; [key: string]: unknown } | null;
|
package/src/index.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import { defineCommercePlugin } from "@unifiedcommerce/core";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { defineCommercePlugin, toolBuilder } from "@unifiedcommerce/core";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { giftCards, giftCardTransactions } from "./schema.js";
|
|
4
|
+
import { GiftCardService } from "./services/gift-card-service.js";
|
|
4
5
|
import {
|
|
5
6
|
buildCheckoutDeductionHook,
|
|
6
7
|
buildCheckoutCompensationHook,
|
|
7
|
-
} from "./hooks/checkout-deduction";
|
|
8
|
-
import { buildGiftCardIssuanceHook } from "./hooks/checkout-issuance";
|
|
9
|
-
import { buildRefundCreditHook } from "./hooks/refund-credit";
|
|
10
|
-
import { buildAdminRoutes } from "./routes/admin";
|
|
11
|
-
import { buildPublicRoutes } from "./routes/public";
|
|
12
|
-
import { buildCustomerRoutes } from "./routes/customer";
|
|
13
|
-
import type { GiftCardPluginOptions
|
|
14
|
-
import { DEFAULT_OPTIONS } from "./types";
|
|
15
|
-
|
|
16
|
-
export type { GiftCardPluginOptions } from "./types";
|
|
17
|
-
export { GiftCardService } from "./services/gift-card-service";
|
|
8
|
+
} from "./hooks/checkout-deduction.js";
|
|
9
|
+
import { buildGiftCardIssuanceHook } from "./hooks/checkout-issuance.js";
|
|
10
|
+
import { buildRefundCreditHook } from "./hooks/refund-credit.js";
|
|
11
|
+
import { buildAdminRoutes } from "./routes/admin.js";
|
|
12
|
+
import { buildPublicRoutes } from "./routes/public.js";
|
|
13
|
+
import { buildCustomerRoutes } from "./routes/customer.js";
|
|
14
|
+
import type { GiftCardPluginOptions } from "./types.js";
|
|
15
|
+
import { DEFAULT_OPTIONS } from "./types.js";
|
|
16
|
+
|
|
17
|
+
export type { GiftCardPluginOptions } from "./types.js";
|
|
18
|
+
export { GiftCardService } from "./services/gift-card-service.js";
|
|
18
19
|
|
|
19
20
|
export function giftCardPlugin(userOptions: GiftCardPluginOptions = {}) {
|
|
20
21
|
const options: Required<GiftCardPluginOptions> = {
|
|
@@ -48,13 +49,83 @@ export function giftCardPlugin(userOptions: GiftCardPluginOptions = {}) {
|
|
|
48
49
|
return [];
|
|
49
50
|
},
|
|
50
51
|
|
|
52
|
+
mcpTools: (ctx) => {
|
|
53
|
+
const db = ctx.database.db;
|
|
54
|
+
if (!db) return [];
|
|
55
|
+
const service = new GiftCardService(
|
|
56
|
+
db,
|
|
57
|
+
ctx.database.transaction,
|
|
58
|
+
options,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const t = toolBuilder("giftcards", "Manage gift cards. Use when checking balances, issuing new cards, looking up card details, or viewing transaction history.");
|
|
62
|
+
|
|
63
|
+
t.action("check_balance", "look up balance by code")
|
|
64
|
+
.input(z.object({
|
|
65
|
+
orgId: z.string().describe("Organization ID"),
|
|
66
|
+
code: z.string().describe("Gift card code"),
|
|
67
|
+
}))
|
|
68
|
+
.handler(async (args) => service.checkBalance(args.orgId, args.code));
|
|
69
|
+
|
|
70
|
+
t.action("issue", "create new card with amount/currency")
|
|
71
|
+
.input(z.object({
|
|
72
|
+
orgId: z.string().describe("Organization ID"),
|
|
73
|
+
amount: z.number().describe("Initial balance in minor units/cents"),
|
|
74
|
+
currency: z.string().describe("Currency code e.g. USD"),
|
|
75
|
+
purchaserId: z.string().describe("Customer who purchased the card").optional(),
|
|
76
|
+
recipientEmail: z.string().describe("Email to deliver the card to").optional(),
|
|
77
|
+
senderName: z.string().describe("Name of the sender").optional(),
|
|
78
|
+
personalMessage: z.string().describe("Personal message for recipient").optional(),
|
|
79
|
+
}))
|
|
80
|
+
.handler(async (args) => {
|
|
81
|
+
const input: { amount: number; currency: string; purchaserId?: string; recipientEmail?: string; senderName?: string; personalMessage?: string } = {
|
|
82
|
+
amount: args.amount,
|
|
83
|
+
currency: args.currency,
|
|
84
|
+
};
|
|
85
|
+
if (args.purchaserId != null) input.purchaserId = args.purchaserId;
|
|
86
|
+
if (args.recipientEmail != null) input.recipientEmail = args.recipientEmail;
|
|
87
|
+
if (args.senderName != null) input.senderName = args.senderName;
|
|
88
|
+
if (args.personalMessage != null) input.personalMessage = args.personalMessage;
|
|
89
|
+
return service.create(args.orgId, input);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
t.action("get", "card details by ID")
|
|
93
|
+
.input(z.object({
|
|
94
|
+
orgId: z.string().describe("Organization ID"),
|
|
95
|
+
id: z.string().describe("Gift card ID"),
|
|
96
|
+
}))
|
|
97
|
+
.handler(async (args) => service.getById(args.orgId, args.id));
|
|
98
|
+
|
|
99
|
+
t.action("list", "all cards with optional filters")
|
|
100
|
+
.input(z.object({
|
|
101
|
+
orgId: z.string().describe("Organization ID"),
|
|
102
|
+
status: z.string().describe("Filter by status: active, exhausted, or disabled").optional(),
|
|
103
|
+
purchaserId: z.string().describe("Filter by purchaser ID").optional(),
|
|
104
|
+
}))
|
|
105
|
+
.handler(async (args) => {
|
|
106
|
+
const filters: { status?: "active" | "exhausted" | "disabled"; purchaserId?: string } = {};
|
|
107
|
+
if (args.status != null) filters.status = args.status as "active" | "exhausted" | "disabled";
|
|
108
|
+
if (args.purchaserId != null) filters.purchaserId = args.purchaserId;
|
|
109
|
+
return service.list(args.orgId, filters);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
t.action("transactions", "credit/debit history for a card")
|
|
113
|
+
.input(z.object({
|
|
114
|
+
orgId: z.string().describe("Organization ID"),
|
|
115
|
+
giftCardId: z.string().describe("Gift card ID"),
|
|
116
|
+
}))
|
|
117
|
+
.handler(async (args) => service.getTransactions(args.orgId, args.giftCardId));
|
|
118
|
+
|
|
119
|
+
return t.build(ctx);
|
|
120
|
+
},
|
|
121
|
+
|
|
51
122
|
routes: (ctx) => {
|
|
52
|
-
const db = ctx.database.db
|
|
123
|
+
const db = ctx.database.db;
|
|
53
124
|
if (!db) return [];
|
|
54
125
|
|
|
55
126
|
const service = new GiftCardService(
|
|
56
127
|
db,
|
|
57
|
-
ctx.database.transaction
|
|
128
|
+
ctx.database.transaction,
|
|
58
129
|
options,
|
|
59
130
|
);
|
|
60
131
|
|
|
@@ -104,15 +175,16 @@ export function giftCardPluginWithHooks(
|
|
|
104
175
|
}),
|
|
105
176
|
|
|
106
177
|
hooks: () => {
|
|
107
|
-
//
|
|
178
|
+
// Lazy proxy: defers to serviceRef.current once routes() initializes it.
|
|
179
|
+
// Uses Reflect.get for type-safe dynamic property access (no index signature needed).
|
|
108
180
|
const lazyService = new Proxy({} as GiftCardService, {
|
|
109
|
-
get(_target, prop) {
|
|
181
|
+
get(_target, prop, receiver) {
|
|
110
182
|
if (!serviceRef.current) {
|
|
111
183
|
throw new Error(
|
|
112
184
|
"Gift card service not initialized — hooks ran before routes()",
|
|
113
185
|
);
|
|
114
186
|
}
|
|
115
|
-
return (serviceRef.current
|
|
187
|
+
return Reflect.get(serviceRef.current, prop, receiver);
|
|
116
188
|
},
|
|
117
189
|
});
|
|
118
190
|
|
|
@@ -125,12 +197,12 @@ export function giftCardPluginWithHooks(
|
|
|
125
197
|
},
|
|
126
198
|
|
|
127
199
|
routes: (ctx) => {
|
|
128
|
-
const db = ctx.database.db
|
|
200
|
+
const db = ctx.database.db;
|
|
129
201
|
if (!db) return [];
|
|
130
202
|
|
|
131
203
|
const service = new GiftCardService(
|
|
132
204
|
db,
|
|
133
|
-
ctx.database.transaction
|
|
205
|
+
ctx.database.transaction,
|
|
134
206
|
options,
|
|
135
207
|
);
|
|
136
208
|
|
package/src/routes/admin.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { router } from "@unifiedcommerce/core";
|
|
2
2
|
import { z } from "@hono/zod-openapi";
|
|
3
|
-
import type { GiftCardService } from "../services/gift-card-service";
|
|
3
|
+
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
4
|
import type { PluginRouteRegistration } from "@unifiedcommerce/core";
|
|
5
|
-
import { formatCode } from "../code-generator";
|
|
5
|
+
import { formatCode } from "../code-generator.js";
|
|
6
6
|
|
|
7
7
|
export function buildAdminRoutes(
|
|
8
8
|
service: GiftCardService,
|
package/src/routes/customer.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { router } from "@unifiedcommerce/core";
|
|
2
|
-
import type { GiftCardService } from "../services/gift-card-service";
|
|
2
|
+
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
3
3
|
import type { PluginRouteRegistration } from "@unifiedcommerce/core";
|
|
4
|
-
import { formatCode } from "../code-generator";
|
|
4
|
+
import { formatCode } from "../code-generator.js";
|
|
5
5
|
|
|
6
6
|
export function buildCustomerRoutes(
|
|
7
7
|
service: GiftCardService,
|
package/src/routes/public.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { router } from "@unifiedcommerce/core";
|
|
2
2
|
import { z } from "@hono/zod-openapi";
|
|
3
|
-
import type { GiftCardService } from "../services/gift-card-service";
|
|
3
|
+
import type { GiftCardService } from "../services/gift-card-service.js";
|
|
4
4
|
import type { PluginRouteRegistration } from "@unifiedcommerce/core";
|
|
5
5
|
|
|
6
6
|
export function buildPublicRoutes(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { eq, desc, and } from "drizzle-orm";
|
|
2
|
-
import { giftCards, giftCardTransactions } from "../schema";
|
|
3
|
-
import type { Db, GiftCard, GiftCardInsert, GiftCardTransaction, GiftCardStatus, TransactionType } from "../types";
|
|
2
|
+
import { giftCards, giftCardTransactions } from "../schema.js";
|
|
3
|
+
import type { Db, GiftCard, GiftCardInsert, GiftCardTransaction, GiftCardStatus, TransactionType } from "../types.js";
|
|
4
4
|
|
|
5
5
|
export class GiftCardRepository {
|
|
6
6
|
constructor(private db: Db) {}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Ok, Err } from "@unifiedcommerce/core";
|
|
2
2
|
import type { PluginResult } from "@unifiedcommerce/core";
|
|
3
|
-
import { generateGiftCardCode, normalizeCode } from "../code-generator";
|
|
4
|
-
import { GiftCardRepository } from "./gift-card-repository";
|
|
3
|
+
import { generateGiftCardCode, normalizeCode } from "../code-generator.js";
|
|
4
|
+
import { GiftCardRepository } from "./gift-card-repository.js";
|
|
5
5
|
import type {
|
|
6
6
|
Db,
|
|
7
7
|
GiftCard,
|
|
@@ -10,7 +10,7 @@ import type {
|
|
|
10
10
|
GiftCardTransaction,
|
|
11
11
|
GiftCardStatus,
|
|
12
12
|
TransactionType,
|
|
13
|
-
} from "../types";
|
|
13
|
+
} from "../types.js";
|
|
14
14
|
|
|
15
15
|
export class GiftCardService {
|
|
16
16
|
private repo: GiftCardRepository;
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type { PluginDb as Db } from "@unifiedcommerce/core";
|
|
2
|
-
import type { giftCards, giftCardTransactions } from "./schema";
|
|
2
|
+
import type { giftCards, giftCardTransactions } from "./schema.js";
|
|
3
3
|
|
|
4
4
|
export type GiftCard = typeof giftCards.$inferSelect;
|
|
5
5
|
export type GiftCardInsert = typeof giftCards.$inferInsert;
|
|
@@ -26,7 +26,7 @@ export interface GiftCardPluginOptions {
|
|
|
26
26
|
|
|
27
27
|
export const DEFAULT_OPTIONS: Required<GiftCardPluginOptions> = {
|
|
28
28
|
codeFormat: "XXXX-XXXX-XXXX-XXXX",
|
|
29
|
-
defaultExpiryDays:
|
|
29
|
+
defaultExpiryDays: 0 as number,
|
|
30
30
|
maxBalancePerCard: 10_000_00,
|
|
31
31
|
emailTemplate: "gift-card-delivery",
|
|
32
32
|
allowPartialRedemption: true,
|