@unifiedcommerce/core 0.0.4 → 0.1.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/auth/auth-schema.d.ts +92 -0
- package/dist/auth/auth-schema.d.ts.map +1 -1
- package/dist/auth/auth-schema.js +7 -0
- package/dist/auth/setup.d.ts.map +1 -1
- package/dist/auth/setup.js +3 -1
- package/package.json +1 -2
- package/src/adapters/console-email.ts +0 -43
- package/src/auth/access.ts +0 -187
- package/src/auth/auth-schema.ts +0 -131
- package/src/auth/middleware.ts +0 -161
- package/src/auth/org.ts +0 -41
- package/src/auth/permissions.ts +0 -28
- package/src/auth/setup.ts +0 -165
- package/src/auth/system-actor.ts +0 -19
- package/src/auth/types.ts +0 -10
- package/src/config/defaults.ts +0 -82
- package/src/config/define-config.ts +0 -53
- package/src/config/types.ts +0 -299
- package/src/generated/plugin-capabilities.d.ts +0 -20
- package/src/generated/plugin-manifest.ts +0 -23
- package/src/generated/plugin-repositories.d.ts +0 -20
- package/src/hooks/checkout-completion.ts +0 -262
- package/src/hooks/checkout.ts +0 -677
- package/src/hooks/order-emails.ts +0 -62
- package/src/index.ts +0 -214
- package/src/interfaces/mcp/agent-prompt.ts +0 -174
- package/src/interfaces/mcp/context-enrichment.ts +0 -177
- package/src/interfaces/mcp/server.ts +0 -617
- package/src/interfaces/mcp/transport.ts +0 -68
- package/src/interfaces/rest/customer-portal.ts +0 -299
- package/src/interfaces/rest/index.ts +0 -74
- package/src/interfaces/rest/router.ts +0 -334
- package/src/interfaces/rest/routes/admin-jobs.ts +0 -58
- package/src/interfaces/rest/routes/audit.ts +0 -50
- package/src/interfaces/rest/routes/carts.ts +0 -89
- package/src/interfaces/rest/routes/catalog.ts +0 -493
- package/src/interfaces/rest/routes/checkout.ts +0 -283
- package/src/interfaces/rest/routes/inventory.ts +0 -70
- package/src/interfaces/rest/routes/media.ts +0 -86
- package/src/interfaces/rest/routes/orders.ts +0 -78
- package/src/interfaces/rest/routes/payments.ts +0 -60
- package/src/interfaces/rest/routes/pricing.ts +0 -57
- package/src/interfaces/rest/routes/promotions.ts +0 -92
- package/src/interfaces/rest/routes/search.ts +0 -71
- package/src/interfaces/rest/routes/webhooks.ts +0 -46
- package/src/interfaces/rest/schemas/admin-jobs.ts +0 -40
- package/src/interfaces/rest/schemas/audit.ts +0 -46
- package/src/interfaces/rest/schemas/carts.ts +0 -125
- package/src/interfaces/rest/schemas/catalog.ts +0 -450
- package/src/interfaces/rest/schemas/checkout.ts +0 -66
- package/src/interfaces/rest/schemas/customer-portal.ts +0 -195
- package/src/interfaces/rest/schemas/inventory.ts +0 -138
- package/src/interfaces/rest/schemas/media.ts +0 -75
- package/src/interfaces/rest/schemas/orders.ts +0 -104
- package/src/interfaces/rest/schemas/pricing.ts +0 -80
- package/src/interfaces/rest/schemas/promotions.ts +0 -110
- package/src/interfaces/rest/schemas/responses.ts +0 -85
- package/src/interfaces/rest/schemas/search.ts +0 -58
- package/src/interfaces/rest/schemas/shared.ts +0 -62
- package/src/interfaces/rest/schemas/webhooks.ts +0 -68
- package/src/interfaces/rest/utils.ts +0 -104
- package/src/interfaces/rest/webhook-router.ts +0 -50
- package/src/kernel/compensation/executor.ts +0 -61
- package/src/kernel/compensation/types.ts +0 -26
- package/src/kernel/database/adapter.ts +0 -13
- package/src/kernel/database/drizzle-db.ts +0 -56
- package/src/kernel/database/migrate.ts +0 -76
- package/src/kernel/database/plugin-types.ts +0 -34
- package/src/kernel/database/schema.ts +0 -49
- package/src/kernel/database/scoped-db.ts +0 -68
- package/src/kernel/database/tx-context.ts +0 -46
- package/src/kernel/error-mapper.ts +0 -15
- package/src/kernel/errors.ts +0 -89
- package/src/kernel/factory/repository-factory.ts +0 -242
- package/src/kernel/hooks/create-context.ts +0 -43
- package/src/kernel/hooks/executor.ts +0 -88
- package/src/kernel/hooks/registry.ts +0 -74
- package/src/kernel/hooks/types.ts +0 -52
- package/src/kernel/http-error.ts +0 -44
- package/src/kernel/jobs/adapter.ts +0 -36
- package/src/kernel/jobs/drizzle-adapter.ts +0 -58
- package/src/kernel/jobs/runner.ts +0 -153
- package/src/kernel/jobs/schema.ts +0 -46
- package/src/kernel/jobs/types.ts +0 -30
- package/src/kernel/local-api.ts +0 -185
- package/src/kernel/plugin/manifest.ts +0 -253
- package/src/kernel/query/executor.ts +0 -184
- package/src/kernel/query/registry.ts +0 -46
- package/src/kernel/result.ts +0 -33
- package/src/kernel/schema/extra-columns.ts +0 -37
- package/src/kernel/service-registry.ts +0 -76
- package/src/kernel/service-timing.ts +0 -89
- package/src/kernel/state-machine/machine.ts +0 -101
- package/src/modules/analytics/drizzle-adapter.ts +0 -426
- package/src/modules/analytics/hooks.ts +0 -11
- package/src/modules/analytics/models.ts +0 -125
- package/src/modules/analytics/repository/index.ts +0 -6
- package/src/modules/analytics/service.ts +0 -245
- package/src/modules/analytics/types.ts +0 -180
- package/src/modules/audit/hooks.ts +0 -78
- package/src/modules/audit/schema.ts +0 -33
- package/src/modules/audit/service.ts +0 -151
- package/src/modules/cart/access.ts +0 -27
- package/src/modules/cart/matcher.ts +0 -26
- package/src/modules/cart/repository/index.ts +0 -234
- package/src/modules/cart/schema.ts +0 -42
- package/src/modules/cart/schemas.ts +0 -38
- package/src/modules/cart/service.ts +0 -541
- package/src/modules/catalog/repository/index.ts +0 -772
- package/src/modules/catalog/schema.ts +0 -203
- package/src/modules/catalog/schemas.ts +0 -104
- package/src/modules/catalog/service.ts +0 -1544
- package/src/modules/customers/repository/index.ts +0 -327
- package/src/modules/customers/schema.ts +0 -64
- package/src/modules/customers/service.ts +0 -171
- package/src/modules/fulfillment/repository/index.ts +0 -426
- package/src/modules/fulfillment/schema.ts +0 -101
- package/src/modules/fulfillment/service.ts +0 -555
- package/src/modules/fulfillment/types.ts +0 -59
- package/src/modules/inventory/repository/index.ts +0 -509
- package/src/modules/inventory/schema.ts +0 -94
- package/src/modules/inventory/schemas.ts +0 -38
- package/src/modules/inventory/service.ts +0 -490
- package/src/modules/media/adapter.ts +0 -17
- package/src/modules/media/repository/index.ts +0 -274
- package/src/modules/media/schema.ts +0 -41
- package/src/modules/media/service.ts +0 -151
- package/src/modules/orders/repository/index.ts +0 -287
- package/src/modules/orders/schema.ts +0 -66
- package/src/modules/orders/service.ts +0 -619
- package/src/modules/orders/stale-order-cleanup.ts +0 -76
- package/src/modules/organization/service.ts +0 -191
- package/src/modules/payments/adapter.ts +0 -47
- package/src/modules/payments/repository/index.ts +0 -6
- package/src/modules/payments/service.ts +0 -107
- package/src/modules/pricing/repository/index.ts +0 -291
- package/src/modules/pricing/schema.ts +0 -71
- package/src/modules/pricing/schemas.ts +0 -38
- package/src/modules/pricing/service.ts +0 -494
- package/src/modules/promotions/repository/index.ts +0 -325
- package/src/modules/promotions/schema.ts +0 -62
- package/src/modules/promotions/schemas.ts +0 -38
- package/src/modules/promotions/service.ts +0 -598
- package/src/modules/search/adapter.ts +0 -57
- package/src/modules/search/hooks.ts +0 -12
- package/src/modules/search/repository/index.ts +0 -6
- package/src/modules/search/service.ts +0 -315
- package/src/modules/shipping/calculator.ts +0 -188
- package/src/modules/shipping/repository/index.ts +0 -6
- package/src/modules/shipping/service.ts +0 -51
- package/src/modules/tax/adapter.ts +0 -60
- package/src/modules/tax/repository/index.ts +0 -6
- package/src/modules/tax/service.ts +0 -53
- package/src/modules/webhooks/hook.ts +0 -34
- package/src/modules/webhooks/repository/index.ts +0 -278
- package/src/modules/webhooks/schema.ts +0 -56
- package/src/modules/webhooks/service.ts +0 -117
- package/src/modules/webhooks/signing.ts +0 -6
- package/src/modules/webhooks/ssrf-guard.ts +0 -71
- package/src/modules/webhooks/tasks.ts +0 -52
- package/src/modules/webhooks/worker.ts +0 -134
- package/src/runtime/commerce.ts +0 -145
- package/src/runtime/kernel.ts +0 -419
- package/src/runtime/logger.ts +0 -36
- package/src/runtime/server.ts +0 -349
- package/src/runtime/shutdown.ts +0 -43
- package/src/test-utils/create-pglite-adapter.ts +0 -129
- package/src/test-utils/create-plugin-test-app.ts +0 -128
- package/src/test-utils/create-repository-test-harness.ts +0 -16
- package/src/test-utils/create-test-config.ts +0 -190
- package/src/test-utils/create-test-kernel.ts +0 -7
- package/src/test-utils/create-test-plugin-context.ts +0 -75
- package/src/test-utils/rest-api-test-utils.ts +0 -265
- package/src/test-utils/test-actors.ts +0 -62
- package/src/test-utils/typed-hooks.ts +0 -54
- package/src/types/commerce-types.ts +0 -34
- package/src/utils/id.ts +0 -3
- package/src/utils/logger.ts +0 -18
- package/src/utils/pagination.ts +0 -22
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
import { eq, and, inArray } from "drizzle-orm";
|
|
2
|
-
import type { TxContext } from "../../../kernel/database/tx-context.js";
|
|
3
|
-
import type {
|
|
4
|
-
DrizzleDatabase,
|
|
5
|
-
DbOrTx,
|
|
6
|
-
} from "../../../kernel/database/drizzle-db.js";
|
|
7
|
-
import { mediaAssets, entityMedia } from "../schema.js";
|
|
8
|
-
|
|
9
|
-
// Infer types from Drizzle schema
|
|
10
|
-
export type MediaAsset = typeof mediaAssets.$inferSelect;
|
|
11
|
-
export type MediaAssetInsert = typeof mediaAssets.$inferInsert;
|
|
12
|
-
export type EntityMedia = typeof entityMedia.$inferSelect;
|
|
13
|
-
export type EntityMediaInsert = typeof entityMedia.$inferInsert;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* MediaRepository provides type-safe database operations for media assets.
|
|
17
|
-
*
|
|
18
|
-
* This repository manages media assets and their associations with entities.
|
|
19
|
-
* All methods support an optional TxContext parameter for transaction participation.
|
|
20
|
-
*/
|
|
21
|
-
export class MediaRepository {
|
|
22
|
-
constructor(private readonly db: DrizzleDatabase) {}
|
|
23
|
-
|
|
24
|
-
private getDb(ctx?: TxContext): DbOrTx {
|
|
25
|
-
return (ctx?.tx as DbOrTx | undefined) ?? this.db;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
29
|
-
// Media Assets
|
|
30
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
31
|
-
|
|
32
|
-
async findAssetById(
|
|
33
|
-
id: string,
|
|
34
|
-
ctx?: TxContext,
|
|
35
|
-
orgId?: string,
|
|
36
|
-
): Promise<MediaAsset | undefined> {
|
|
37
|
-
const db = this.getDb(ctx);
|
|
38
|
-
const conditions = [eq(mediaAssets.id, id)];
|
|
39
|
-
if (orgId) {
|
|
40
|
-
conditions.push(eq(mediaAssets.organizationId, orgId));
|
|
41
|
-
}
|
|
42
|
-
const rows = await db
|
|
43
|
-
.select()
|
|
44
|
-
.from(mediaAssets)
|
|
45
|
-
.where(and(...conditions));
|
|
46
|
-
return rows[0];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async findAssetByStorageKey(
|
|
50
|
-
storageKey: string,
|
|
51
|
-
ctx?: TxContext,
|
|
52
|
-
): Promise<MediaAsset | undefined> {
|
|
53
|
-
const db = this.getDb(ctx);
|
|
54
|
-
const rows = await db
|
|
55
|
-
.select()
|
|
56
|
-
.from(mediaAssets)
|
|
57
|
-
.where(eq(mediaAssets.storageKey, storageKey));
|
|
58
|
-
return rows[0];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async findAssetsByIds(ids: string[], ctx?: TxContext): Promise<MediaAsset[]> {
|
|
62
|
-
if (ids.length === 0) return [];
|
|
63
|
-
const db = this.getDb(ctx);
|
|
64
|
-
return db.select().from(mediaAssets).where(inArray(mediaAssets.id, ids));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async createAsset(
|
|
68
|
-
data: MediaAssetInsert,
|
|
69
|
-
ctx?: TxContext,
|
|
70
|
-
): Promise<MediaAsset> {
|
|
71
|
-
const db = this.getDb(ctx);
|
|
72
|
-
const rows = await db.insert(mediaAssets).values(data).returning();
|
|
73
|
-
return rows[0]!;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async updateAsset(
|
|
77
|
-
id: string,
|
|
78
|
-
data: Partial<Omit<MediaAssetInsert, "id">>,
|
|
79
|
-
ctx?: TxContext,
|
|
80
|
-
): Promise<MediaAsset | undefined> {
|
|
81
|
-
const db = this.getDb(ctx);
|
|
82
|
-
const rows = await db
|
|
83
|
-
.update(mediaAssets)
|
|
84
|
-
.set(data)
|
|
85
|
-
.where(eq(mediaAssets.id, id))
|
|
86
|
-
.returning();
|
|
87
|
-
return rows[0];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async deleteAsset(id: string, ctx?: TxContext): Promise<boolean> {
|
|
91
|
-
const db = this.getDb(ctx);
|
|
92
|
-
const result = await db
|
|
93
|
-
.delete(mediaAssets)
|
|
94
|
-
.where(eq(mediaAssets.id, id))
|
|
95
|
-
.returning();
|
|
96
|
-
return result.length > 0;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
100
|
-
// Entity Media (Associations)
|
|
101
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
102
|
-
|
|
103
|
-
async findEntityMedia(
|
|
104
|
-
entityId: string,
|
|
105
|
-
variantId?: string,
|
|
106
|
-
ctx?: TxContext,
|
|
107
|
-
): Promise<EntityMedia[]> {
|
|
108
|
-
const db = this.getDb(ctx);
|
|
109
|
-
|
|
110
|
-
if (variantId === undefined) {
|
|
111
|
-
// Find media for entity only (not variant-specific)
|
|
112
|
-
const rows = await db
|
|
113
|
-
.select()
|
|
114
|
-
.from(entityMedia)
|
|
115
|
-
.where(eq(entityMedia.entityId, entityId));
|
|
116
|
-
return rows.filter((r) => r.variantId === null);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return db
|
|
120
|
-
.select()
|
|
121
|
-
.from(entityMedia)
|
|
122
|
-
.where(
|
|
123
|
-
and(
|
|
124
|
-
eq(entityMedia.entityId, entityId),
|
|
125
|
-
eq(entityMedia.variantId, variantId),
|
|
126
|
-
),
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async findEntityMediaByRole(
|
|
131
|
-
entityId: string,
|
|
132
|
-
role: EntityMedia["role"],
|
|
133
|
-
variantId?: string,
|
|
134
|
-
ctx?: TxContext,
|
|
135
|
-
): Promise<EntityMedia[]> {
|
|
136
|
-
const db = this.getDb(ctx);
|
|
137
|
-
const conditions = [
|
|
138
|
-
eq(entityMedia.entityId, entityId),
|
|
139
|
-
eq(entityMedia.role, role),
|
|
140
|
-
];
|
|
141
|
-
|
|
142
|
-
if (variantId !== undefined) {
|
|
143
|
-
conditions.push(eq(entityMedia.variantId, variantId));
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const rows = await db
|
|
147
|
-
.select()
|
|
148
|
-
.from(entityMedia)
|
|
149
|
-
.where(and(...conditions));
|
|
150
|
-
|
|
151
|
-
if (variantId === undefined) {
|
|
152
|
-
return rows.filter((r) => r.variantId === null);
|
|
153
|
-
}
|
|
154
|
-
return rows;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async findPrimaryMedia(
|
|
158
|
-
entityId: string,
|
|
159
|
-
variantId?: string,
|
|
160
|
-
ctx?: TxContext,
|
|
161
|
-
): Promise<EntityMedia | undefined> {
|
|
162
|
-
const media = await this.findEntityMediaByRole(
|
|
163
|
-
entityId,
|
|
164
|
-
"primary",
|
|
165
|
-
variantId,
|
|
166
|
-
ctx,
|
|
167
|
-
);
|
|
168
|
-
return media[0];
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async createEntityMedia(
|
|
172
|
-
data: EntityMediaInsert,
|
|
173
|
-
ctx?: TxContext,
|
|
174
|
-
): Promise<EntityMedia> {
|
|
175
|
-
const db = this.getDb(ctx);
|
|
176
|
-
const rows = await db.insert(entityMedia).values(data).returning();
|
|
177
|
-
return rows[0]!;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async createEntityMediaBatch(
|
|
181
|
-
data: EntityMediaInsert[],
|
|
182
|
-
ctx?: TxContext,
|
|
183
|
-
): Promise<EntityMedia[]> {
|
|
184
|
-
if (data.length === 0) return [];
|
|
185
|
-
const db = this.getDb(ctx);
|
|
186
|
-
return db.insert(entityMedia).values(data).returning();
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async updateEntityMediaSortOrder(
|
|
190
|
-
entityId: string,
|
|
191
|
-
mediaAssetId: string,
|
|
192
|
-
sortOrder: number,
|
|
193
|
-
variantId?: string,
|
|
194
|
-
ctx?: TxContext,
|
|
195
|
-
): Promise<EntityMedia | undefined> {
|
|
196
|
-
const db = this.getDb(ctx);
|
|
197
|
-
const conditions = [
|
|
198
|
-
eq(entityMedia.entityId, entityId),
|
|
199
|
-
eq(entityMedia.mediaAssetId, mediaAssetId),
|
|
200
|
-
];
|
|
201
|
-
|
|
202
|
-
if (variantId !== undefined) {
|
|
203
|
-
conditions.push(eq(entityMedia.variantId, variantId));
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const rows = await db
|
|
207
|
-
.update(entityMedia)
|
|
208
|
-
.set({ sortOrder })
|
|
209
|
-
.where(and(...conditions))
|
|
210
|
-
.returning();
|
|
211
|
-
|
|
212
|
-
if (variantId === undefined) {
|
|
213
|
-
return rows.find((r) => r.variantId === null);
|
|
214
|
-
}
|
|
215
|
-
return rows[0];
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
async removeEntityMedia(
|
|
219
|
-
entityId: string,
|
|
220
|
-
mediaAssetId: string,
|
|
221
|
-
variantId?: string,
|
|
222
|
-
ctx?: TxContext,
|
|
223
|
-
): Promise<boolean> {
|
|
224
|
-
const db = this.getDb(ctx);
|
|
225
|
-
const conditions = [
|
|
226
|
-
eq(entityMedia.entityId, entityId),
|
|
227
|
-
eq(entityMedia.mediaAssetId, mediaAssetId),
|
|
228
|
-
];
|
|
229
|
-
|
|
230
|
-
if (variantId !== undefined) {
|
|
231
|
-
conditions.push(eq(entityMedia.variantId, variantId));
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const result = await db
|
|
235
|
-
.delete(entityMedia)
|
|
236
|
-
.where(and(...conditions))
|
|
237
|
-
.returning();
|
|
238
|
-
|
|
239
|
-
if (variantId === undefined) {
|
|
240
|
-
return result.some((r) => r.variantId === null);
|
|
241
|
-
}
|
|
242
|
-
return result.length > 0;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
async removeAllEntityMedia(entityId: string, ctx?: TxContext): Promise<void> {
|
|
246
|
-
const db = this.getDb(ctx);
|
|
247
|
-
await db.delete(entityMedia).where(eq(entityMedia.entityId, entityId));
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
async removeAllMediaByAssetId(
|
|
251
|
-
mediaAssetId: string,
|
|
252
|
-
ctx?: TxContext,
|
|
253
|
-
): Promise<void> {
|
|
254
|
-
const db = this.getDb(ctx);
|
|
255
|
-
await db
|
|
256
|
-
.delete(entityMedia)
|
|
257
|
-
.where(eq(entityMedia.mediaAssetId, mediaAssetId));
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
261
|
-
// Aggregates
|
|
262
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
263
|
-
|
|
264
|
-
async findAssetsForEntity(
|
|
265
|
-
entityId: string,
|
|
266
|
-
variantId?: string,
|
|
267
|
-
ctx?: TxContext,
|
|
268
|
-
): Promise<MediaAsset[]> {
|
|
269
|
-
const associations = await this.findEntityMedia(entityId, variantId, ctx);
|
|
270
|
-
const assetIds = associations.map((a) => a.mediaAssetId);
|
|
271
|
-
if (assetIds.length === 0) return [];
|
|
272
|
-
return this.findAssetsByIds(assetIds, ctx);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { index, integer, jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
|
|
2
|
-
import { organization } from "../../auth/auth-schema.js";
|
|
3
|
-
import { sellableEntities, variants } from "../catalog/schema.js";
|
|
4
|
-
|
|
5
|
-
export const mediaAssets = pgTable(
|
|
6
|
-
"media_assets",
|
|
7
|
-
{
|
|
8
|
-
id: uuid("id").defaultRandom().primaryKey(),
|
|
9
|
-
organizationId: text("organization_id")
|
|
10
|
-
.notNull()
|
|
11
|
-
.references(() => organization.id, { onDelete: "cascade" }),
|
|
12
|
-
storageKey: text("storage_key").notNull(),
|
|
13
|
-
filename: text("filename").notNull(),
|
|
14
|
-
contentType: text("content_type").notNull(),
|
|
15
|
-
size: integer("size").notNull(),
|
|
16
|
-
width: integer("width"),
|
|
17
|
-
height: integer("height"),
|
|
18
|
-
alt: text("alt"),
|
|
19
|
-
metadata: jsonb("metadata").$type<Record<string, unknown>>().default({}),
|
|
20
|
-
uploadedAt: timestamp("uploaded_at", { withTimezone: true }).defaultNow().notNull(),
|
|
21
|
-
},
|
|
22
|
-
(table) => ({
|
|
23
|
-
orgIdx: index("idx_media_assets_org").on(table.organizationId),
|
|
24
|
-
}),
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
export const entityMedia = pgTable("entity_media", {
|
|
28
|
-
entityId: uuid("entity_id")
|
|
29
|
-
.references(() => sellableEntities.id, { onDelete: "cascade" })
|
|
30
|
-
.notNull(),
|
|
31
|
-
variantId: uuid("variant_id").references(() => variants.id, {
|
|
32
|
-
onDelete: "cascade",
|
|
33
|
-
}),
|
|
34
|
-
mediaAssetId: uuid("media_asset_id")
|
|
35
|
-
.references(() => mediaAssets.id, { onDelete: "cascade" })
|
|
36
|
-
.notNull(),
|
|
37
|
-
role: text("role", {
|
|
38
|
-
enum: ["primary", "gallery", "thumbnail", "video", "document"],
|
|
39
|
-
}).notNull(),
|
|
40
|
-
sortOrder: integer("sort_order").notNull().default(0),
|
|
41
|
-
});
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { resolveOrgId } from "../../auth/org.js";
|
|
2
|
-
import type { Actor } from "../../auth/types.js";
|
|
3
|
-
import type { StorageAdapter, StoredFile } from "./adapter.js";
|
|
4
|
-
import { Err, Ok, type Result } from "../../kernel/result.js";
|
|
5
|
-
import {
|
|
6
|
-
CommerceNotFoundError,
|
|
7
|
-
CommerceValidationError,
|
|
8
|
-
} from "../../kernel/errors.js";
|
|
9
|
-
import type { MediaRepository } from "./repository/index.js";
|
|
10
|
-
import type { CatalogRepository } from "../catalog/repository/index.js";
|
|
11
|
-
import type { TxContext } from "../../kernel/database/tx-context.js";
|
|
12
|
-
import { makeId } from "../../utils/id.js";
|
|
13
|
-
|
|
14
|
-
export interface UploadMediaInput {
|
|
15
|
-
filename: string;
|
|
16
|
-
contentType: string;
|
|
17
|
-
data: ArrayBuffer;
|
|
18
|
-
alt?: string;
|
|
19
|
-
metadata?: Record<string, unknown>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface AttachMediaInput {
|
|
23
|
-
entityId: string;
|
|
24
|
-
mediaAssetId: string;
|
|
25
|
-
role: "primary" | "gallery" | "thumbnail" | "video" | "document";
|
|
26
|
-
variantId?: string;
|
|
27
|
-
sortOrder?: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface MediaServiceDeps {
|
|
31
|
-
repository: MediaRepository;
|
|
32
|
-
catalogRepository: CatalogRepository;
|
|
33
|
-
storage: StorageAdapter;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class MediaService {
|
|
37
|
-
private readonly repo: MediaRepository;
|
|
38
|
-
private readonly catalogRepo: CatalogRepository;
|
|
39
|
-
|
|
40
|
-
constructor(private deps: MediaServiceDeps) {
|
|
41
|
-
this.repo = deps.repository;
|
|
42
|
-
this.catalogRepo = deps.catalogRepository;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async upload(
|
|
46
|
-
input: UploadMediaInput,
|
|
47
|
-
actor?: Actor | null,
|
|
48
|
-
ctx?: TxContext,
|
|
49
|
-
): Promise<Result<{ id: string; url: string }>> {
|
|
50
|
-
if (!input.filename || !input.contentType) {
|
|
51
|
-
return Err(
|
|
52
|
-
new CommerceValidationError("filename and contentType are required."),
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const orgId = resolveOrgId(actor ?? ctx?.actor ?? null);
|
|
57
|
-
|
|
58
|
-
const id = makeId();
|
|
59
|
-
const key = `${new Date().getFullYear()}/${id}-${input.filename}`;
|
|
60
|
-
const uploaded = await this.deps.storage.upload(
|
|
61
|
-
key,
|
|
62
|
-
input.data,
|
|
63
|
-
input.contentType,
|
|
64
|
-
);
|
|
65
|
-
if (!uploaded.ok) return uploaded as Result<never>;
|
|
66
|
-
|
|
67
|
-
await this.repo.createAsset(
|
|
68
|
-
{
|
|
69
|
-
organizationId: orgId,
|
|
70
|
-
id,
|
|
71
|
-
storageKey: key,
|
|
72
|
-
filename: input.filename,
|
|
73
|
-
contentType: input.contentType,
|
|
74
|
-
size: input.data.byteLength,
|
|
75
|
-
metadata: input.metadata ?? {},
|
|
76
|
-
uploadedAt: new Date(),
|
|
77
|
-
...(input.alt !== undefined ? { alt: input.alt } : {}),
|
|
78
|
-
},
|
|
79
|
-
ctx,
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
const url = await this.deps.storage.getUrl(key);
|
|
83
|
-
if (!url.ok) return url as Result<never>;
|
|
84
|
-
|
|
85
|
-
return Ok({ id, url: url.value });
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async getUrl(id: string, ctx?: TxContext): Promise<Result<string>> {
|
|
89
|
-
const asset = await this.repo.findAssetById(id, ctx);
|
|
90
|
-
if (!asset) return Err(new CommerceNotFoundError("Media asset not found."));
|
|
91
|
-
return this.deps.storage.getUrl(asset.storageKey);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async getSignedUrl(
|
|
95
|
-
id: string,
|
|
96
|
-
expiresIn = 60 * 15,
|
|
97
|
-
ctx?: TxContext,
|
|
98
|
-
): Promise<Result<string>> {
|
|
99
|
-
const asset = await this.repo.findAssetById(id, ctx);
|
|
100
|
-
if (!asset) return Err(new CommerceNotFoundError("Media asset not found."));
|
|
101
|
-
return this.deps.storage.getSignedUrl(asset.storageKey, expiresIn);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async delete(id: string, ctx?: TxContext): Promise<Result<void>> {
|
|
105
|
-
const asset = await this.repo.findAssetById(id, ctx);
|
|
106
|
-
if (!asset) return Err(new CommerceNotFoundError("Media asset not found."));
|
|
107
|
-
|
|
108
|
-
const deleted = await this.deps.storage.delete(asset.storageKey);
|
|
109
|
-
if (!deleted.ok) return deleted;
|
|
110
|
-
|
|
111
|
-
await this.repo.removeAllMediaByAssetId(id, ctx);
|
|
112
|
-
await this.repo.deleteAsset(id, ctx);
|
|
113
|
-
return Ok(undefined);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async list(prefix = ""): Promise<Result<StoredFile[]>> {
|
|
117
|
-
const listed = await this.deps.storage.list(prefix);
|
|
118
|
-
if (!listed.ok) return listed;
|
|
119
|
-
return Ok(listed.value);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async attachToEntity(
|
|
123
|
-
input: AttachMediaInput,
|
|
124
|
-
ctx?: TxContext,
|
|
125
|
-
): Promise<Result<void>> {
|
|
126
|
-
const entity = await this.catalogRepo.findEntityById(input.entityId, ctx);
|
|
127
|
-
if (!entity) {
|
|
128
|
-
return Err(new CommerceNotFoundError("Entity not found."));
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const asset = await this.repo.findAssetById(input.mediaAssetId, ctx);
|
|
132
|
-
if (!asset) {
|
|
133
|
-
return Err(new CommerceNotFoundError("Media asset not found."));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
await this.repo.createEntityMedia(
|
|
137
|
-
{
|
|
138
|
-
entityId: input.entityId,
|
|
139
|
-
mediaAssetId: input.mediaAssetId,
|
|
140
|
-
role: input.role,
|
|
141
|
-
sortOrder: input.sortOrder ?? 0,
|
|
142
|
-
...(input.variantId !== undefined
|
|
143
|
-
? { variantId: input.variantId }
|
|
144
|
-
: {}),
|
|
145
|
-
},
|
|
146
|
-
ctx,
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
return Ok(undefined);
|
|
150
|
-
}
|
|
151
|
-
}
|