@oneclick.dev/cms-core-modules 0.0.73 → 0.0.75
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/server-handlers.cjs.js +1 -0
- package/dist/server-handlers.mjs +898 -0
- package/package.json +7 -4
- package/src/appointments/server.ts +0 -195
- package/src/googleAnalytics/server.ts +0 -1188
- package/src/products/server.ts +0 -146
package/src/products/server.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { createRouter, defineEventHandler, createError, getRouterParam, getQuery } from 'h3'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Dependencies injected by the CMS when creating this handler.
|
|
5
|
-
* Modules never import CMS internals directly — they receive them here.
|
|
6
|
-
*/
|
|
7
|
-
export interface ModuleHandlerDeps {
|
|
8
|
-
/** Initialize a Firebase Admin app for a project-level integration */
|
|
9
|
-
initFirebase: (event: any, integrationId: string) => Promise<any>
|
|
10
|
-
/** Normalize Firestore Timestamp fields to plain dates */
|
|
11
|
-
normalizeTimestamps: (data: any) => any
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Products module server handler factory.
|
|
16
|
-
*
|
|
17
|
-
* Routes (relative to `/api/v1/modules/:instanceId/`):
|
|
18
|
-
* GET /products → list all products
|
|
19
|
-
* GET /products/:productId → get single product
|
|
20
|
-
* GET /empty-stock → get products with 0 stock (API-mode tool)
|
|
21
|
-
*/
|
|
22
|
-
export function createServerHandler(deps: ModuleHandlerDeps) {
|
|
23
|
-
const { initFirebase, normalizeTimestamps } = deps
|
|
24
|
-
const router = createRouter()
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Resolve the Firebase app + collection name from the module instance config.
|
|
28
|
-
*/
|
|
29
|
-
async function getFirebaseContext(event: any) {
|
|
30
|
-
const { supabase, instanceId } = event.context.module
|
|
31
|
-
|
|
32
|
-
const { data: moduleRow, error } = await supabase
|
|
33
|
-
.from('project_modules')
|
|
34
|
-
.select('config')
|
|
35
|
-
.eq('id', instanceId)
|
|
36
|
-
.single()
|
|
37
|
-
|
|
38
|
-
if (error || !moduleRow?.config) {
|
|
39
|
-
throw createError({ statusCode: 500, statusMessage: 'Failed to load module config.' })
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const config = moduleRow.config as Record<string, any>
|
|
43
|
-
const integrationId = config.project as string
|
|
44
|
-
const collection = (config.productCollection as string) || 'products'
|
|
45
|
-
|
|
46
|
-
if (!integrationId) {
|
|
47
|
-
throw createError({ statusCode: 400, statusMessage: 'Products module has no Firebase integration configured.' })
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const firebase = await initFirebase(event, integrationId)
|
|
51
|
-
return { firebase, collection }
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// ── GET /products ──────────────────────────────────
|
|
55
|
-
router.get('/products', defineEventHandler(async (event) => {
|
|
56
|
-
try {
|
|
57
|
-
const { firebase, collection } = await getFirebaseContext(event)
|
|
58
|
-
const snapshot = await firebase.firestore().collection(collection).get()
|
|
59
|
-
|
|
60
|
-
return snapshot.docs.map((doc: any) => normalizeTimestamps({
|
|
61
|
-
id: doc.id,
|
|
62
|
-
...doc.data(),
|
|
63
|
-
}))
|
|
64
|
-
} catch (err: any) {
|
|
65
|
-
console.error('Products handler — list error:', err)
|
|
66
|
-
throw createError({
|
|
67
|
-
statusCode: err.statusCode || 500,
|
|
68
|
-
statusMessage: err.statusMessage || 'Failed to list products',
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
}))
|
|
72
|
-
|
|
73
|
-
// ── GET /products/:productId ───────────────────────
|
|
74
|
-
router.get('/products/:productId', defineEventHandler(async (event) => {
|
|
75
|
-
const productId = getRouterParam(event, 'productId')
|
|
76
|
-
if (!productId) {
|
|
77
|
-
throw createError({ statusCode: 400, statusMessage: 'Product ID is required.' })
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
const { firebase, collection } = await getFirebaseContext(event)
|
|
82
|
-
const snapshot = await firebase.firestore().collection(collection).doc(productId).get()
|
|
83
|
-
|
|
84
|
-
if (!snapshot.exists) {
|
|
85
|
-
throw createError({ statusCode: 404, statusMessage: 'Product not found.' })
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return normalizeTimestamps({
|
|
89
|
-
id: snapshot.id,
|
|
90
|
-
...snapshot.data(),
|
|
91
|
-
})
|
|
92
|
-
} catch (err: any) {
|
|
93
|
-
console.error('Products handler — get error:', err)
|
|
94
|
-
throw createError({
|
|
95
|
-
statusCode: err.statusCode || 500,
|
|
96
|
-
statusMessage: err.statusMessage || 'Failed to get product',
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
}))
|
|
100
|
-
|
|
101
|
-
// ── GET /empty-stock?category=... ──────────────────
|
|
102
|
-
router.get('/empty-stock', defineEventHandler(async (event) => {
|
|
103
|
-
const query = getQuery(event)
|
|
104
|
-
const category = query.category as string | undefined
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
const { firebase, collection } = await getFirebaseContext(event)
|
|
108
|
-
let fbQuery: FirebaseFirestore.Query = firebase.firestore().collection(collection)
|
|
109
|
-
.where('stock', '==', 0)
|
|
110
|
-
|
|
111
|
-
if (category) {
|
|
112
|
-
fbQuery = fbQuery.where('collections', 'array-contains', category)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const snapshot = await fbQuery.get()
|
|
116
|
-
const products = snapshot.docs.map((doc: any) => normalizeTimestamps({
|
|
117
|
-
id: doc.id,
|
|
118
|
-
...doc.data(),
|
|
119
|
-
}))
|
|
120
|
-
|
|
121
|
-
return {
|
|
122
|
-
count: products.length,
|
|
123
|
-
products: products.map((p: any) => ({
|
|
124
|
-
id: p.id,
|
|
125
|
-
title: p.title,
|
|
126
|
-
slug: p.slug,
|
|
127
|
-
stock: p.stock,
|
|
128
|
-
price: p.price,
|
|
129
|
-
currency: p.currency,
|
|
130
|
-
status: p.status,
|
|
131
|
-
})),
|
|
132
|
-
}
|
|
133
|
-
} catch (err: any) {
|
|
134
|
-
console.error('Products handler — empty-stock error:', err)
|
|
135
|
-
throw createError({
|
|
136
|
-
statusCode: err.statusCode || 500,
|
|
137
|
-
statusMessage: err.statusMessage || 'Failed to query empty stock',
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
}))
|
|
141
|
-
|
|
142
|
-
// ── POST /products/:productId/change-status ──────────────────
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return router.handler
|
|
146
|
-
}
|