@vicacha-devs/medusa-plugin-stories 0.0.0-stage → 0.1.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/.medusa/server/src/__tests__/stories-plugin.spec.js +358 -0
- package/.medusa/server/src/admin/i18n/index.js +35 -0
- package/.medusa/server/src/admin/i18n/json/ar.json +196 -0
- package/.medusa/server/src/admin/i18n/json/de.json +196 -0
- package/.medusa/server/src/admin/i18n/json/en.json +196 -0
- package/.medusa/server/src/admin/i18n/json/es.json +196 -0
- package/.medusa/server/src/admin/i18n/json/fr.json +196 -0
- package/.medusa/server/src/admin/i18n/json/it.json +196 -0
- package/.medusa/server/src/admin/i18n/json/nl.json +196 -0
- package/.medusa/server/src/admin/i18n/json/pl.json +196 -0
- package/.medusa/server/src/admin/i18n/json/pt.json +196 -0
- package/.medusa/server/src/admin/i18n/json/tr.json +196 -0
- package/.medusa/server/src/admin/index.cjs +4699 -0
- package/.medusa/server/src/admin/index.js +4699 -0
- package/.medusa/server/src/api/admin/plugin/route.js +4 -0
- package/.medusa/server/src/api/admin/stories/[id]/hotspots/route.js +14 -0
- package/.medusa/server/src/api/admin/stories/[id]/route.js +27 -0
- package/.medusa/server/src/api/admin/stories/[id]/stickers/route.js +14 -0
- package/.medusa/server/src/api/admin/stories/analytics/route.js +7 -0
- package/.medusa/server/src/api/admin/stories/groups/[id]/route.js +23 -0
- package/.medusa/server/src/api/admin/stories/groups/route.js +15 -0
- package/.medusa/server/src/api/admin/stories/route.js +21 -0
- package/.medusa/server/src/api/middlewares.js +58 -0
- package/.medusa/server/src/api/store/plugin/route.js +4 -0
- package/.medusa/server/src/api/store/stories/[id]/events/route.js +12 -0
- package/.medusa/server/src/api/store/stories/[id]/interact/route.js +14 -0
- package/.medusa/server/src/api/store/stories/route.js +33 -0
- package/.medusa/server/src/api/validators.js +136 -0
- package/.medusa/server/src/hooks/index.js +213 -0
- package/.medusa/server/src/hooks/use-sdk-primitives.js +93 -0
- package/.medusa/server/src/index.js +6 -0
- package/.medusa/server/src/jobs/expire-ephemeral-stories.js +10 -0
- package/.medusa/server/src/modules/index.js +7 -0
- package/.medusa/server/src/modules/stories/index.js +3 -0
- package/.medusa/server/src/modules/story/index.js +23 -0
- package/.medusa/server/src/modules/story/migrations/Migration20260514000000.js +181 -0
- package/.medusa/server/src/modules/story/models/index.js +141 -0
- package/.medusa/server/src/modules/story/service.js +673 -0
- package/.medusa/server/src/modules/story/types.js +3 -0
- package/.medusa/server/src/modules/story/utils/security.js +76 -0
- package/.medusa/server/src/rbac.js +75 -0
- package/.medusa/server/src/sdk/index.js +148 -0
- package/.medusa/server/src/subscribers/product-deleted.js +11 -0
- package/.medusa/server/src/types/index.js +2 -0
- package/.medusa/server/src/ui/index.js +5 -0
- package/.medusa/server/src/ui/story-hotspot-pin.js +43 -0
- package/.medusa/server/src/ui/story-player-modal.js +104 -0
- package/.medusa/server/src/ui/story-product-sheet.js +7 -0
- package/.medusa/server/src/ui/story-rings-bar.js +42 -0
- package/.medusa/server/src/workflows/index.js +50 -0
- package/LICENSE +21 -0
- package/README.md +246 -2
- package/package.json +132 -3
- package/src/__tests__/medusa-shim.mjs +533 -0
- package/src/__tests__/register-ts-resolver.mjs +141 -0
- package/src/__tests__/stories-plugin.spec.ts +526 -0
- package/src/admin/README.md +94 -0
- package/src/admin/components/analytics-tab.tsx +225 -0
- package/src/admin/components/canvas-editor-tab.tsx +77 -0
- package/src/admin/components/hotspot-form-section.tsx +155 -0
- package/src/admin/components/index.ts +15 -0
- package/src/admin/components/overview-cards.tsx +96 -0
- package/src/admin/components/product-stories-card.tsx +123 -0
- package/src/admin/components/rbac-guard.tsx +153 -0
- package/src/admin/components/slide-form-section.tsx +187 -0
- package/src/admin/components/slide-preview-canvas.tsx +166 -0
- package/src/admin/components/slide-timeline-section.tsx +126 -0
- package/src/admin/components/sticker-form-section.tsx +201 -0
- package/src/admin/components/sticker-list-section.tsx +113 -0
- package/src/admin/components/stickers-tab.tsx +34 -0
- package/src/admin/components/story-ring-form.tsx +241 -0
- package/src/admin/components/story-rings-tab.tsx +204 -0
- package/src/admin/components/story-rings-table.tsx +222 -0
- package/src/admin/components/studio-header.tsx +67 -0
- package/src/admin/hooks/use-product-stories.ts +71 -0
- package/src/admin/hooks/use-stories-studio.ts +439 -0
- package/src/admin/i18n/README.md +84 -0
- package/src/admin/i18n/index.ts +36 -0
- package/src/admin/i18n/json/ar.json +196 -0
- package/src/admin/i18n/json/de.json +196 -0
- package/src/admin/i18n/json/en.json +196 -0
- package/src/admin/i18n/json/es.json +196 -0
- package/src/admin/i18n/json/fr.json +196 -0
- package/src/admin/i18n/json/it.json +196 -0
- package/src/admin/i18n/json/nl.json +196 -0
- package/src/admin/i18n/json/pl.json +196 -0
- package/src/admin/i18n/json/pt.json +196 -0
- package/src/admin/i18n/json/tr.json +196 -0
- package/src/admin/routes/stories/analytics/page.tsx +66 -0
- package/src/admin/routes/stories/canvas/page.tsx +101 -0
- package/src/admin/routes/stories/page.tsx +164 -0
- package/src/admin/routes/stories/rings/@create/page.tsx +61 -0
- package/src/admin/routes/stories/rings/page.tsx +88 -0
- package/src/admin/routes/stories/stickers/page.tsx +74 -0
- package/src/admin/tsconfig.json +23 -0
- package/src/admin/utils/theme-presets.ts +94 -0
- package/src/admin/vite-env.d.ts +1 -0
- package/src/admin/widgets/product-stories-widget.tsx +32 -0
- package/src/api/README.md +61 -0
- package/src/api/admin/plugin/route.ts +5 -0
- package/src/api/admin/stories/[id]/hotspots/route.ts +17 -0
- package/src/api/admin/stories/[id]/route.ts +40 -0
- package/src/api/admin/stories/[id]/stickers/route.ts +20 -0
- package/src/api/admin/stories/analytics/route.ts +8 -0
- package/src/api/admin/stories/groups/[id]/route.ts +30 -0
- package/src/api/admin/stories/groups/route.ts +18 -0
- package/src/api/admin/stories/route.ts +31 -0
- package/src/api/middlewares.ts +73 -0
- package/src/api/store/plugin/route.ts +5 -0
- package/src/api/store/stories/[id]/events/route.ts +14 -0
- package/src/api/store/stories/[id]/interact/route.ts +18 -0
- package/src/api/store/stories/route.ts +44 -0
- package/src/api/validators.ts +166 -0
- package/src/hooks/index.ts +419 -0
- package/src/hooks/use-sdk-primitives.ts +114 -0
- package/src/index.ts +14 -0
- package/src/jobs/README.md +16 -0
- package/src/jobs/expire-ephemeral-stories.ts +14 -0
- package/src/links/README.md +18 -0
- package/src/modules/README.md +46 -0
- package/src/modules/index.ts +15 -0
- package/src/modules/stories/index.ts +11 -0
- package/src/modules/story/index.ts +34 -0
- package/src/modules/story/migrations/Migration20260514000000.ts +196 -0
- package/src/modules/story/models/index.ts +146 -0
- package/src/modules/story/service.ts +1052 -0
- package/src/modules/story/types.ts +190 -0
- package/src/modules/story/utils/security.ts +100 -0
- package/src/providers/README.md +14 -0
- package/src/rbac.ts +89 -0
- package/src/sdk/index.ts +233 -0
- package/src/subscribers/README.md +16 -0
- package/src/subscribers/product-deleted.ts +15 -0
- package/src/types/index.ts +1 -0
- package/src/types/medusa-ui.d.ts +451 -0
- package/src/ui/index.ts +4 -0
- package/src/ui/story-hotspot-pin.tsx +122 -0
- package/src/ui/story-player-modal.tsx +361 -0
- package/src/ui/story-product-sheet.tsx +85 -0
- package/src/ui/story-rings-bar.tsx +113 -0
- package/src/workflows/README.md +19 -0
- package/src/workflows/index.ts +217 -0
- package/tsconfig.json +39 -0
|
@@ -0,0 +1,1052 @@
|
|
|
1
|
+
import { MedusaError, MedusaService } from "@medusajs/framework/utils"
|
|
2
|
+
import { StoriesModels } from "./models"
|
|
3
|
+
import type {
|
|
4
|
+
StoriesAnalyticsOverview,
|
|
5
|
+
StoriesPluginOptions,
|
|
6
|
+
StoryEngagementEventType,
|
|
7
|
+
StoryGroupAnalyticsSummary,
|
|
8
|
+
StoryGroupDTO,
|
|
9
|
+
StoryHotspotDTO,
|
|
10
|
+
StoryMediaType,
|
|
11
|
+
StoryPlacementZone,
|
|
12
|
+
StoryRingTheme,
|
|
13
|
+
StorySlideDTO,
|
|
14
|
+
StoryStickerDTO,
|
|
15
|
+
StoryStickerOption,
|
|
16
|
+
StoryStickerType,
|
|
17
|
+
} from "./types"
|
|
18
|
+
import {
|
|
19
|
+
clampCoordinatePercent,
|
|
20
|
+
sanitizeHttpUrl,
|
|
21
|
+
sanitizeRingColor,
|
|
22
|
+
slugifyStoryHandle,
|
|
23
|
+
} from "./utils/security"
|
|
24
|
+
|
|
25
|
+
export class StoriesModuleService extends MedusaService(StoriesModels) {
|
|
26
|
+
protected readonly options_: StoriesPluginOptions
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
_container: Record<string, unknown> = {},
|
|
30
|
+
options: StoriesPluginOptions = {}
|
|
31
|
+
) {
|
|
32
|
+
super(...arguments)
|
|
33
|
+
this.options_ = options ?? {}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ─── Story Groups / Highlights Rings ───────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
async createGroup(input: {
|
|
39
|
+
title: string
|
|
40
|
+
handle?: string
|
|
41
|
+
subtitle?: string | null
|
|
42
|
+
avatar_url?: string | null
|
|
43
|
+
ring_theme?: StoryRingTheme
|
|
44
|
+
custom_ring_color?: string | null
|
|
45
|
+
placement?: StoryPlacementZone
|
|
46
|
+
target_entity_id?: string | null
|
|
47
|
+
is_pinned?: boolean
|
|
48
|
+
is_ephemeral?: boolean
|
|
49
|
+
is_active?: boolean
|
|
50
|
+
display_order?: number
|
|
51
|
+
metadata?: Record<string, unknown> | null
|
|
52
|
+
}): Promise<StoryGroupDTO> {
|
|
53
|
+
const title = String(input.title || "").trim()
|
|
54
|
+
if (!title) {
|
|
55
|
+
throw new MedusaError(
|
|
56
|
+
MedusaError.Types.INVALID_DATA,
|
|
57
|
+
"Story group title is required."
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const handle = slugifyStoryHandle(input.handle || title)
|
|
62
|
+
const avatarUrl = sanitizeHttpUrl(input.avatar_url, "avatar_url")
|
|
63
|
+
const customRingColor = sanitizeRingColor(input.custom_ring_color)
|
|
64
|
+
|
|
65
|
+
const group = await this.createStoryGroups({
|
|
66
|
+
title,
|
|
67
|
+
handle,
|
|
68
|
+
subtitle: input.subtitle?.trim() || null,
|
|
69
|
+
avatar_url: avatarUrl,
|
|
70
|
+
ring_theme:
|
|
71
|
+
input.ring_theme ??
|
|
72
|
+
this.options_.defaultRingTheme ??
|
|
73
|
+
this.options_.default_ring_theme ??
|
|
74
|
+
"instagram",
|
|
75
|
+
custom_ring_color: customRingColor,
|
|
76
|
+
placement: input.placement ?? "home",
|
|
77
|
+
target_entity_id: input.target_entity_id?.trim() || null,
|
|
78
|
+
is_pinned: input.is_pinned ?? true,
|
|
79
|
+
is_ephemeral: input.is_ephemeral ?? false,
|
|
80
|
+
is_active: input.is_active ?? true,
|
|
81
|
+
display_order: input.display_order ?? 0,
|
|
82
|
+
metadata: input.metadata ?? null,
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return group as unknown as StoryGroupDTO
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async updateGroup(
|
|
89
|
+
id: string,
|
|
90
|
+
patch: Partial<{
|
|
91
|
+
title: string
|
|
92
|
+
handle: string
|
|
93
|
+
subtitle: string | null
|
|
94
|
+
avatar_url: string | null
|
|
95
|
+
ring_theme: StoryRingTheme
|
|
96
|
+
custom_ring_color: string | null
|
|
97
|
+
placement: StoryPlacementZone
|
|
98
|
+
target_entity_id: string | null
|
|
99
|
+
is_pinned: boolean
|
|
100
|
+
is_ephemeral: boolean
|
|
101
|
+
is_active: boolean
|
|
102
|
+
display_order: number
|
|
103
|
+
metadata: Record<string, unknown> | null
|
|
104
|
+
}>
|
|
105
|
+
): Promise<StoryGroupDTO> {
|
|
106
|
+
const existing = await this.listStoryGroups({ id })
|
|
107
|
+
if (!existing?.[0]) {
|
|
108
|
+
throw new MedusaError(
|
|
109
|
+
MedusaError.Types.NOT_FOUND,
|
|
110
|
+
`Story group "${id}" not found.`
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const updateData: Record<string, unknown> = { id }
|
|
115
|
+
if (patch.title !== undefined) {
|
|
116
|
+
const trimmed = patch.title.trim()
|
|
117
|
+
if (!trimmed) {
|
|
118
|
+
throw new MedusaError(
|
|
119
|
+
MedusaError.Types.INVALID_DATA,
|
|
120
|
+
"Story group title cannot be empty."
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
updateData.title = trimmed
|
|
124
|
+
}
|
|
125
|
+
if (patch.handle !== undefined) {
|
|
126
|
+
updateData.handle = slugifyStoryHandle(patch.handle)
|
|
127
|
+
}
|
|
128
|
+
if (patch.subtitle !== undefined) {
|
|
129
|
+
updateData.subtitle = patch.subtitle?.trim() || null
|
|
130
|
+
}
|
|
131
|
+
if (patch.avatar_url !== undefined) {
|
|
132
|
+
updateData.avatar_url = sanitizeHttpUrl(patch.avatar_url, "avatar_url")
|
|
133
|
+
}
|
|
134
|
+
if (patch.ring_theme !== undefined) {
|
|
135
|
+
updateData.ring_theme = patch.ring_theme
|
|
136
|
+
}
|
|
137
|
+
if (patch.custom_ring_color !== undefined) {
|
|
138
|
+
updateData.custom_ring_color = sanitizeRingColor(patch.custom_ring_color)
|
|
139
|
+
}
|
|
140
|
+
if (patch.placement !== undefined) {
|
|
141
|
+
updateData.placement = patch.placement
|
|
142
|
+
}
|
|
143
|
+
if (patch.target_entity_id !== undefined) {
|
|
144
|
+
updateData.target_entity_id = patch.target_entity_id?.trim() || null
|
|
145
|
+
}
|
|
146
|
+
if (patch.is_pinned !== undefined) {
|
|
147
|
+
updateData.is_pinned = patch.is_pinned
|
|
148
|
+
}
|
|
149
|
+
if (patch.is_ephemeral !== undefined) {
|
|
150
|
+
updateData.is_ephemeral = patch.is_ephemeral
|
|
151
|
+
}
|
|
152
|
+
if (patch.is_active !== undefined) {
|
|
153
|
+
updateData.is_active = patch.is_active
|
|
154
|
+
}
|
|
155
|
+
if (patch.display_order !== undefined) {
|
|
156
|
+
updateData.display_order = patch.display_order
|
|
157
|
+
}
|
|
158
|
+
if (patch.metadata !== undefined) {
|
|
159
|
+
updateData.metadata = patch.metadata
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const updated = await this.updateStoryGroups(updateData)
|
|
163
|
+
return updated as unknown as StoryGroupDTO
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async deleteGroup(id: string): Promise<{ id: string; deleted: boolean }> {
|
|
167
|
+
const existing = await this.listStoryGroups({ id })
|
|
168
|
+
if (!existing?.[0]) {
|
|
169
|
+
throw new MedusaError(
|
|
170
|
+
MedusaError.Types.NOT_FOUND,
|
|
171
|
+
`Story group "${id}" not found.`
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
const groupSlides = await this.listStories({ group_id: id })
|
|
175
|
+
for (const slide of groupSlides ?? []) {
|
|
176
|
+
await this.updateStories({ id: slide.id, group_id: null })
|
|
177
|
+
}
|
|
178
|
+
await this.deleteStoryGroups(id)
|
|
179
|
+
return { id, deleted: true }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ─── Story Slides + Hotspots + Stickers ────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
async createSlide(input: {
|
|
185
|
+
group_id?: string | null
|
|
186
|
+
title: string
|
|
187
|
+
caption?: string | null
|
|
188
|
+
media_type?: StoryMediaType
|
|
189
|
+
media_url: string
|
|
190
|
+
thumbnail_url?: string | null
|
|
191
|
+
duration_ms?: number
|
|
192
|
+
link_url?: string | null
|
|
193
|
+
link_text?: string | null
|
|
194
|
+
is_active?: boolean
|
|
195
|
+
display_order?: number
|
|
196
|
+
starts_at?: string | Date | null
|
|
197
|
+
expires_at?: string | Date | null
|
|
198
|
+
hotspots?: Array<{
|
|
199
|
+
product_id: string
|
|
200
|
+
variant_id?: string | null
|
|
201
|
+
product_title: string
|
|
202
|
+
product_handle?: string | null
|
|
203
|
+
product_thumbnail?: string | null
|
|
204
|
+
price_formatted?: string | null
|
|
205
|
+
price_amount?: number | null
|
|
206
|
+
currency_code?: string | null
|
|
207
|
+
x_percent: number
|
|
208
|
+
y_percent: number
|
|
209
|
+
label?: string | null
|
|
210
|
+
badge_style?: "pill" | "card" | "dot"
|
|
211
|
+
}>
|
|
212
|
+
stickers?: Array<{
|
|
213
|
+
type: StoryStickerType
|
|
214
|
+
prompt: string
|
|
215
|
+
x_percent?: number
|
|
216
|
+
y_percent?: number
|
|
217
|
+
options?: Array<{ id?: string; label: string; votes?: number }> | null
|
|
218
|
+
coupon_code?: string | null
|
|
219
|
+
countdown_until?: string | null
|
|
220
|
+
}>
|
|
221
|
+
metadata?: Record<string, unknown> | null
|
|
222
|
+
}): Promise<StorySlideDTO> {
|
|
223
|
+
const title = String(input.title || "").trim()
|
|
224
|
+
if (!title) {
|
|
225
|
+
throw new MedusaError(
|
|
226
|
+
MedusaError.Types.INVALID_DATA,
|
|
227
|
+
"Story title is required."
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const mediaUrl = sanitizeHttpUrl(input.media_url, "media_url")
|
|
232
|
+
if (!mediaUrl) {
|
|
233
|
+
throw new MedusaError(
|
|
234
|
+
MedusaError.Types.INVALID_DATA,
|
|
235
|
+
"Story media_url is required."
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const thumbnailUrl = sanitizeHttpUrl(input.thumbnail_url, "thumbnail_url")
|
|
240
|
+
const linkUrl = sanitizeHttpUrl(input.link_url, "link_url")
|
|
241
|
+
|
|
242
|
+
const defaultDuration =
|
|
243
|
+
this.options_.defaultSlideDurationMs ??
|
|
244
|
+
this.options_.default_slide_duration_ms ??
|
|
245
|
+
5000
|
|
246
|
+
|
|
247
|
+
const durationMs = Math.min(
|
|
248
|
+
60000,
|
|
249
|
+
Math.max(1000, input.duration_ms ?? defaultDuration)
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
const slide = await this.createStories({
|
|
253
|
+
group_id: input.group_id || null,
|
|
254
|
+
title,
|
|
255
|
+
caption: input.caption?.trim() || null,
|
|
256
|
+
media_type: input.media_type ?? "image",
|
|
257
|
+
media_url: mediaUrl,
|
|
258
|
+
thumbnail_url: thumbnailUrl,
|
|
259
|
+
duration_ms: durationMs,
|
|
260
|
+
link_url: linkUrl,
|
|
261
|
+
link_text: input.link_text?.trim() || null,
|
|
262
|
+
is_active: input.is_active ?? true,
|
|
263
|
+
display_order: input.display_order ?? 0,
|
|
264
|
+
starts_at: input.starts_at ? new Date(input.starts_at) : null,
|
|
265
|
+
expires_at: input.expires_at ? new Date(input.expires_at) : null,
|
|
266
|
+
views_count: 0,
|
|
267
|
+
completions_count: 0,
|
|
268
|
+
clicks_count: 0,
|
|
269
|
+
cart_conversions_count: 0,
|
|
270
|
+
metadata: input.metadata ?? null,
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
const createdHotspots: StoryHotspotDTO[] = []
|
|
274
|
+
for (const hs of input.hotspots ?? []) {
|
|
275
|
+
const createdHs = await this.addHotspot(slide.id, hs)
|
|
276
|
+
createdHotspots.push(createdHs)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const createdStickers: StoryStickerDTO[] = []
|
|
280
|
+
for (const stk of input.stickers ?? []) {
|
|
281
|
+
const createdStk = await this.addSticker(slide.id, stk)
|
|
282
|
+
createdStickers.push(createdStk)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
...(slide as unknown as StorySlideDTO),
|
|
287
|
+
hotspots: createdHotspots,
|
|
288
|
+
stickers: createdStickers,
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async updateSlide(
|
|
293
|
+
id: string,
|
|
294
|
+
patch: Partial<{
|
|
295
|
+
group_id: string | null
|
|
296
|
+
title: string
|
|
297
|
+
caption: string | null
|
|
298
|
+
media_type: StoryMediaType
|
|
299
|
+
media_url: string
|
|
300
|
+
thumbnail_url: string | null
|
|
301
|
+
duration_ms: number
|
|
302
|
+
link_url: string | null
|
|
303
|
+
link_text: string | null
|
|
304
|
+
is_active: boolean
|
|
305
|
+
display_order: number
|
|
306
|
+
starts_at: string | Date | null
|
|
307
|
+
expires_at: string | Date | null
|
|
308
|
+
metadata: Record<string, unknown> | null
|
|
309
|
+
}>
|
|
310
|
+
): Promise<StorySlideDTO> {
|
|
311
|
+
const existing = await this.listStories({ id })
|
|
312
|
+
if (!existing?.[0]) {
|
|
313
|
+
throw new MedusaError(
|
|
314
|
+
MedusaError.Types.NOT_FOUND,
|
|
315
|
+
`Story "${id}" not found.`
|
|
316
|
+
)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const updateData: Record<string, unknown> = { id }
|
|
320
|
+
if (patch.group_id !== undefined) updateData.group_id = patch.group_id
|
|
321
|
+
if (patch.title !== undefined) {
|
|
322
|
+
const trimmed = patch.title.trim()
|
|
323
|
+
if (!trimmed) {
|
|
324
|
+
throw new MedusaError(
|
|
325
|
+
MedusaError.Types.INVALID_DATA,
|
|
326
|
+
"Story title cannot be empty."
|
|
327
|
+
)
|
|
328
|
+
}
|
|
329
|
+
updateData.title = trimmed
|
|
330
|
+
}
|
|
331
|
+
if (patch.caption !== undefined) {
|
|
332
|
+
updateData.caption = patch.caption?.trim() || null
|
|
333
|
+
}
|
|
334
|
+
if (patch.media_type !== undefined) {
|
|
335
|
+
updateData.media_type = patch.media_type
|
|
336
|
+
}
|
|
337
|
+
if (patch.media_url !== undefined) {
|
|
338
|
+
const url = sanitizeHttpUrl(patch.media_url, "media_url")
|
|
339
|
+
if (!url) {
|
|
340
|
+
throw new MedusaError(
|
|
341
|
+
MedusaError.Types.INVALID_DATA,
|
|
342
|
+
"media_url cannot be empty."
|
|
343
|
+
)
|
|
344
|
+
}
|
|
345
|
+
updateData.media_url = url
|
|
346
|
+
}
|
|
347
|
+
if (patch.thumbnail_url !== undefined) {
|
|
348
|
+
updateData.thumbnail_url = sanitizeHttpUrl(
|
|
349
|
+
patch.thumbnail_url,
|
|
350
|
+
"thumbnail_url"
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
if (patch.duration_ms !== undefined) {
|
|
354
|
+
updateData.duration_ms = Math.min(
|
|
355
|
+
60000,
|
|
356
|
+
Math.max(1000, patch.duration_ms)
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
if (patch.link_url !== undefined) {
|
|
360
|
+
updateData.link_url = sanitizeHttpUrl(patch.link_url, "link_url")
|
|
361
|
+
}
|
|
362
|
+
if (patch.link_text !== undefined) {
|
|
363
|
+
updateData.link_text = patch.link_text?.trim() || null
|
|
364
|
+
}
|
|
365
|
+
if (patch.is_active !== undefined) {
|
|
366
|
+
updateData.is_active = patch.is_active
|
|
367
|
+
}
|
|
368
|
+
if (patch.display_order !== undefined) {
|
|
369
|
+
updateData.display_order = patch.display_order
|
|
370
|
+
}
|
|
371
|
+
if (patch.starts_at !== undefined) {
|
|
372
|
+
updateData.starts_at = patch.starts_at ? new Date(patch.starts_at) : null
|
|
373
|
+
}
|
|
374
|
+
if (patch.expires_at !== undefined) {
|
|
375
|
+
updateData.expires_at = patch.expires_at
|
|
376
|
+
? new Date(patch.expires_at)
|
|
377
|
+
: null
|
|
378
|
+
}
|
|
379
|
+
if (patch.metadata !== undefined) {
|
|
380
|
+
updateData.metadata = patch.metadata
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const updated = await this.updateStories(updateData)
|
|
384
|
+
return this.hydrateSlide(updated as unknown as StorySlideDTO)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
async deleteSlide(id: string): Promise<{ id: string; deleted: boolean }> {
|
|
388
|
+
const existing = await this.listStories({ id })
|
|
389
|
+
if (!existing?.[0]) {
|
|
390
|
+
throw new MedusaError(
|
|
391
|
+
MedusaError.Types.NOT_FOUND,
|
|
392
|
+
`Story "${id}" not found.`
|
|
393
|
+
)
|
|
394
|
+
}
|
|
395
|
+
const [hotspots, stickers, events] = await Promise.all([
|
|
396
|
+
this.listStoryHotspots({ story_id: id }),
|
|
397
|
+
this.listStoryStickers({ story_id: id }),
|
|
398
|
+
this.listStoryEvents({ story_id: id }),
|
|
399
|
+
])
|
|
400
|
+
for (const hs of hotspots ?? []) {
|
|
401
|
+
await this.deleteStoryHotspots(hs.id)
|
|
402
|
+
}
|
|
403
|
+
for (const stk of stickers ?? []) {
|
|
404
|
+
await this.deleteStoryStickers(stk.id)
|
|
405
|
+
}
|
|
406
|
+
for (const evt of events ?? []) {
|
|
407
|
+
await this.deleteStoryEvents(evt.id)
|
|
408
|
+
}
|
|
409
|
+
await this.deleteStories(id)
|
|
410
|
+
return { id, deleted: true }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async addHotspot(
|
|
414
|
+
storyId: string,
|
|
415
|
+
input: {
|
|
416
|
+
product_id: string
|
|
417
|
+
variant_id?: string | null
|
|
418
|
+
product_title: string
|
|
419
|
+
product_handle?: string | null
|
|
420
|
+
product_thumbnail?: string | null
|
|
421
|
+
price_formatted?: string | null
|
|
422
|
+
price_amount?: number | null
|
|
423
|
+
currency_code?: string | null
|
|
424
|
+
x_percent: number
|
|
425
|
+
y_percent: number
|
|
426
|
+
label?: string | null
|
|
427
|
+
badge_style?: "pill" | "card" | "dot"
|
|
428
|
+
}
|
|
429
|
+
): Promise<StoryHotspotDTO> {
|
|
430
|
+
if (!input.product_id || !input.product_title) {
|
|
431
|
+
throw new MedusaError(
|
|
432
|
+
MedusaError.Types.INVALID_DATA,
|
|
433
|
+
"Hotspot requires product_id and product_title."
|
|
434
|
+
)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const existingStories = await this.listStories({ id: storyId })
|
|
438
|
+
if (!existingStories?.[0]) {
|
|
439
|
+
throw new MedusaError(
|
|
440
|
+
MedusaError.Types.NOT_FOUND,
|
|
441
|
+
`Story "${storyId}" not found.`
|
|
442
|
+
)
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const maxHotspots =
|
|
446
|
+
this.options_.maxHotspotsPerSlide ??
|
|
447
|
+
this.options_.max_hotspots_per_slide ??
|
|
448
|
+
20
|
|
449
|
+
const currentHotspots = await this.listStoryHotspots({ story_id: storyId })
|
|
450
|
+
if ((currentHotspots?.length ?? 0) >= maxHotspots) {
|
|
451
|
+
throw new MedusaError(
|
|
452
|
+
MedusaError.Types.INVALID_DATA,
|
|
453
|
+
`Story "${storyId}" has reached the maximum of ${maxHotspots} hotspots per slide.`
|
|
454
|
+
)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const x = clampCoordinatePercent(input.x_percent)
|
|
458
|
+
const y = clampCoordinatePercent(input.y_percent)
|
|
459
|
+
const thumb = sanitizeHttpUrl(input.product_thumbnail, "product_thumbnail")
|
|
460
|
+
|
|
461
|
+
const hotspot = await this.createStoryHotspots({
|
|
462
|
+
story_id: storyId,
|
|
463
|
+
product_id: input.product_id.trim(),
|
|
464
|
+
variant_id: input.variant_id?.trim() || null,
|
|
465
|
+
product_title: input.product_title.trim(),
|
|
466
|
+
product_handle: input.product_handle?.trim() || null,
|
|
467
|
+
product_thumbnail: thumb,
|
|
468
|
+
price_formatted: input.price_formatted?.trim() || null,
|
|
469
|
+
price_amount: input.price_amount ?? null,
|
|
470
|
+
currency_code: input.currency_code?.trim().toLowerCase() || null,
|
|
471
|
+
x_percent: x,
|
|
472
|
+
y_percent: y,
|
|
473
|
+
label: input.label?.trim() || null,
|
|
474
|
+
badge_style: input.badge_style ?? "pill",
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
return hotspot as unknown as StoryHotspotDTO
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
async removeHotspot(
|
|
481
|
+
hotspotId: string,
|
|
482
|
+
storyId?: string
|
|
483
|
+
): Promise<{ id: string; deleted: boolean }> {
|
|
484
|
+
const filter: Record<string, unknown> = { id: hotspotId }
|
|
485
|
+
if (storyId) {
|
|
486
|
+
filter.story_id = storyId
|
|
487
|
+
}
|
|
488
|
+
const existing = await this.listStoryHotspots(filter)
|
|
489
|
+
if (!existing?.[0]) {
|
|
490
|
+
throw new MedusaError(
|
|
491
|
+
MedusaError.Types.NOT_FOUND,
|
|
492
|
+
`Hotspot "${hotspotId}" not found.`
|
|
493
|
+
)
|
|
494
|
+
}
|
|
495
|
+
await this.deleteStoryHotspots(hotspotId)
|
|
496
|
+
return { id: hotspotId, deleted: true }
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
async addSticker(
|
|
500
|
+
storyId: string,
|
|
501
|
+
input: {
|
|
502
|
+
type: StoryStickerType
|
|
503
|
+
prompt: string
|
|
504
|
+
x_percent?: number
|
|
505
|
+
y_percent?: number
|
|
506
|
+
options?: Array<{ id?: string; label: string; votes?: number }> | null
|
|
507
|
+
coupon_code?: string | null
|
|
508
|
+
countdown_until?: string | null
|
|
509
|
+
metadata?: Record<string, unknown> | null
|
|
510
|
+
}
|
|
511
|
+
): Promise<StoryStickerDTO> {
|
|
512
|
+
const prompt = String(input.prompt || "").trim()
|
|
513
|
+
if (!prompt) {
|
|
514
|
+
throw new MedusaError(
|
|
515
|
+
MedusaError.Types.INVALID_DATA,
|
|
516
|
+
"Sticker prompt is required."
|
|
517
|
+
)
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const existingStories = await this.listStories({ id: storyId })
|
|
521
|
+
if (!existingStories?.[0]) {
|
|
522
|
+
throw new MedusaError(
|
|
523
|
+
MedusaError.Types.NOT_FOUND,
|
|
524
|
+
`Story "${storyId}" not found.`
|
|
525
|
+
)
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const x = clampCoordinatePercent(input.x_percent ?? 50)
|
|
529
|
+
const y = clampCoordinatePercent(input.y_percent ?? 25)
|
|
530
|
+
|
|
531
|
+
const normalizedOptions: StoryStickerOption[] | null = input.options
|
|
532
|
+
? input.options.map((opt, idx) => ({
|
|
533
|
+
id: opt.id?.trim() || `opt_${idx + 1}`,
|
|
534
|
+
label: String(opt.label || "").trim(),
|
|
535
|
+
votes: Number(opt.votes ?? 0),
|
|
536
|
+
}))
|
|
537
|
+
: null
|
|
538
|
+
|
|
539
|
+
const sticker = await this.createStoryStickers({
|
|
540
|
+
story_id: storyId,
|
|
541
|
+
type: input.type,
|
|
542
|
+
prompt,
|
|
543
|
+
x_percent: x,
|
|
544
|
+
y_percent: y,
|
|
545
|
+
options: normalizedOptions as unknown as Record<string, unknown>,
|
|
546
|
+
coupon_code: input.coupon_code?.trim().toUpperCase() || null,
|
|
547
|
+
countdown_until: input.countdown_until || null,
|
|
548
|
+
total_interactions: 0,
|
|
549
|
+
metadata: input.metadata ?? null,
|
|
550
|
+
})
|
|
551
|
+
|
|
552
|
+
return {
|
|
553
|
+
...(sticker as unknown as StoryStickerDTO),
|
|
554
|
+
options: normalizedOptions,
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
async removeSticker(
|
|
559
|
+
stickerId: string,
|
|
560
|
+
storyId?: string
|
|
561
|
+
): Promise<{ id: string; deleted: boolean }> {
|
|
562
|
+
const filter: Record<string, unknown> = { id: stickerId }
|
|
563
|
+
if (storyId) {
|
|
564
|
+
filter.story_id = storyId
|
|
565
|
+
}
|
|
566
|
+
const existing = await this.listStoryStickers(filter)
|
|
567
|
+
if (!existing?.[0]) {
|
|
568
|
+
throw new MedusaError(
|
|
569
|
+
MedusaError.Types.NOT_FOUND,
|
|
570
|
+
`Sticker "${stickerId}" not found.`
|
|
571
|
+
)
|
|
572
|
+
}
|
|
573
|
+
await this.deleteStoryStickers(stickerId)
|
|
574
|
+
return { id: stickerId, deleted: true }
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
async interactWithSticker(params: {
|
|
578
|
+
storyId: string
|
|
579
|
+
stickerId: string
|
|
580
|
+
optionId?: string
|
|
581
|
+
responseText?: string
|
|
582
|
+
sessionId?: string
|
|
583
|
+
}): Promise<StoryStickerDTO> {
|
|
584
|
+
const stickers = await this.listStoryStickers({
|
|
585
|
+
id: params.stickerId,
|
|
586
|
+
story_id: params.storyId,
|
|
587
|
+
})
|
|
588
|
+
const sticker = stickers?.[0]
|
|
589
|
+
if (!sticker) {
|
|
590
|
+
throw new MedusaError(
|
|
591
|
+
MedusaError.Types.NOT_FOUND,
|
|
592
|
+
`Sticker "${params.stickerId}" not found on story "${params.storyId}".`
|
|
593
|
+
)
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
let options = Array.isArray(sticker.options)
|
|
597
|
+
? [...(sticker.options as unknown as StoryStickerOption[])]
|
|
598
|
+
: null
|
|
599
|
+
|
|
600
|
+
if (sticker.type === "poll" && options && params.optionId) {
|
|
601
|
+
const targetIdx = options.findIndex((o) => o.id === params.optionId)
|
|
602
|
+
if (targetIdx === -1) {
|
|
603
|
+
throw new MedusaError(
|
|
604
|
+
MedusaError.Types.INVALID_DATA,
|
|
605
|
+
`Poll option "${params.optionId}" not found.`
|
|
606
|
+
)
|
|
607
|
+
}
|
|
608
|
+
options[targetIdx] = {
|
|
609
|
+
...options[targetIdx],
|
|
610
|
+
votes: (options[targetIdx].votes ?? 0) + 1,
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const totalInteractions = Number(sticker.total_interactions ?? 0) + 1
|
|
615
|
+
const updated = await this.updateStoryStickers({
|
|
616
|
+
id: sticker.id,
|
|
617
|
+
options: options as unknown as Record<string, unknown>,
|
|
618
|
+
total_interactions: totalInteractions,
|
|
619
|
+
})
|
|
620
|
+
|
|
621
|
+
const slides = await this.listStories({ id: params.storyId })
|
|
622
|
+
const groupId = slides?.[0]?.group_id ?? null
|
|
623
|
+
|
|
624
|
+
await this.createStoryEvents({
|
|
625
|
+
story_id: params.storyId,
|
|
626
|
+
group_id: groupId,
|
|
627
|
+
event_type: "sticker_interact",
|
|
628
|
+
sticker_id: sticker.id,
|
|
629
|
+
session_id: params.sessionId ?? null,
|
|
630
|
+
metadata: {
|
|
631
|
+
option_id: params.optionId ?? null,
|
|
632
|
+
response_text: params.responseText
|
|
633
|
+
? String(params.responseText).slice(0, 280)
|
|
634
|
+
: null,
|
|
635
|
+
},
|
|
636
|
+
})
|
|
637
|
+
|
|
638
|
+
return {
|
|
639
|
+
...(updated as unknown as StoryStickerDTO),
|
|
640
|
+
options,
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// ─── Querying & Hydration (Admin & Storefront) ─────────────────────────────
|
|
645
|
+
|
|
646
|
+
async hydrateSlide(rawSlide: StorySlideDTO): Promise<StorySlideDTO> {
|
|
647
|
+
const [hotspots, stickers] = await Promise.all([
|
|
648
|
+
this.listStoryHotspots({ story_id: rawSlide.id }),
|
|
649
|
+
this.listStoryStickers({ story_id: rawSlide.id }),
|
|
650
|
+
])
|
|
651
|
+
|
|
652
|
+
return {
|
|
653
|
+
...rawSlide,
|
|
654
|
+
hotspots: (hotspots ?? []) as unknown as StoryHotspotDTO[],
|
|
655
|
+
stickers: (stickers ?? []).map((s) => ({
|
|
656
|
+
...(s as unknown as StoryStickerDTO),
|
|
657
|
+
options: Array.isArray(s.options)
|
|
658
|
+
? (s.options as unknown as StoryStickerOption[])
|
|
659
|
+
: null,
|
|
660
|
+
})),
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Backward-compatible `listActive()` method matching `@bambiste/medusa-stories`,
|
|
666
|
+
* enhanced with start/expiration window checks and hydrated hotspots & stickers.
|
|
667
|
+
*/
|
|
668
|
+
async listActive(filters?: {
|
|
669
|
+
group_id?: string
|
|
670
|
+
product_id?: string
|
|
671
|
+
}): Promise<StorySlideDTO[]> {
|
|
672
|
+
const now = new Date()
|
|
673
|
+
const queryFilter: Record<string, unknown> = { is_active: true }
|
|
674
|
+
if (filters?.group_id) {
|
|
675
|
+
queryFilter.group_id = filters.group_id
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const all = await this.listStories(queryFilter)
|
|
679
|
+
const activeSlides = all
|
|
680
|
+
.filter((s) => {
|
|
681
|
+
if (s.starts_at && new Date(s.starts_at) > now) return false
|
|
682
|
+
if (s.expires_at && new Date(s.expires_at) <= now) return false
|
|
683
|
+
return true
|
|
684
|
+
})
|
|
685
|
+
.toSorted((a, b) => (a.display_order ?? 0) - (b.display_order ?? 0))
|
|
686
|
+
|
|
687
|
+
const hydrated = await Promise.all(
|
|
688
|
+
activeSlides.map((s) => this.hydrateSlide(s as unknown as StorySlideDTO))
|
|
689
|
+
)
|
|
690
|
+
|
|
691
|
+
if (filters?.product_id) {
|
|
692
|
+
return hydrated.filter((s) =>
|
|
693
|
+
(s.hotspots ?? []).some((h) => h.product_id === filters.product_id)
|
|
694
|
+
)
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
return hydrated
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
async listStorefrontGroups(filters?: {
|
|
701
|
+
placement?: StoryPlacementZone
|
|
702
|
+
target_entity_id?: string
|
|
703
|
+
group_id?: string
|
|
704
|
+
product_id?: string
|
|
705
|
+
collection_id?: string
|
|
706
|
+
}): Promise<StoryGroupDTO[]> {
|
|
707
|
+
const effectiveTargetEntityId =
|
|
708
|
+
filters?.target_entity_id ?? filters?.collection_id
|
|
709
|
+
const groups = await this.listStoryGroups({ is_active: true })
|
|
710
|
+
const filteredGroups = groups
|
|
711
|
+
.filter((g) => {
|
|
712
|
+
if (filters?.group_id && g.id !== filters.group_id) {
|
|
713
|
+
return false
|
|
714
|
+
}
|
|
715
|
+
if (
|
|
716
|
+
filters?.placement &&
|
|
717
|
+
g.placement !== filters.placement &&
|
|
718
|
+
g.placement !== "global"
|
|
719
|
+
) {
|
|
720
|
+
return false
|
|
721
|
+
}
|
|
722
|
+
if (
|
|
723
|
+
effectiveTargetEntityId &&
|
|
724
|
+
g.target_entity_id &&
|
|
725
|
+
g.target_entity_id !== effectiveTargetEntityId
|
|
726
|
+
) {
|
|
727
|
+
return false
|
|
728
|
+
}
|
|
729
|
+
return true
|
|
730
|
+
})
|
|
731
|
+
.toSorted((a, b) => {
|
|
732
|
+
if (a.is_pinned !== b.is_pinned) return a.is_pinned ? -1 : 1
|
|
733
|
+
return (a.display_order ?? 0) - (b.display_order ?? 0)
|
|
734
|
+
})
|
|
735
|
+
|
|
736
|
+
const activeSlides = await this.listActive({
|
|
737
|
+
group_id: filters?.group_id,
|
|
738
|
+
product_id: filters?.product_id,
|
|
739
|
+
})
|
|
740
|
+
const slidesByGroup = new Map<string, StorySlideDTO[]>()
|
|
741
|
+
for (const slide of activeSlides) {
|
|
742
|
+
if (slide.group_id) {
|
|
743
|
+
const list = slidesByGroup.get(slide.group_id) ?? []
|
|
744
|
+
list.push(slide)
|
|
745
|
+
slidesByGroup.set(slide.group_id, list)
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const hydratedGroups = filteredGroups.map((g) => ({
|
|
750
|
+
...(g as unknown as StoryGroupDTO),
|
|
751
|
+
slides: slidesByGroup.get(g.id) ?? [],
|
|
752
|
+
}))
|
|
753
|
+
|
|
754
|
+
if (filters?.product_id) {
|
|
755
|
+
return hydratedGroups.filter((g) => (g.slides?.length ?? 0) > 0)
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
return hydratedGroups
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
async listAdminStudioState(filters?: { product_id?: string }): Promise<{
|
|
762
|
+
groups: StoryGroupDTO[]
|
|
763
|
+
slides: StorySlideDTO[]
|
|
764
|
+
}> {
|
|
765
|
+
const [rawGroups, rawSlides] = await Promise.all([
|
|
766
|
+
this.listStoryGroups({}),
|
|
767
|
+
this.listStories({}),
|
|
768
|
+
])
|
|
769
|
+
|
|
770
|
+
const sortedSlides = [...rawSlides].toSorted(
|
|
771
|
+
(a, b) => (a.display_order ?? 0) - (b.display_order ?? 0)
|
|
772
|
+
)
|
|
773
|
+
const hydratedSlides = await Promise.all(
|
|
774
|
+
sortedSlides.map((s) => this.hydrateSlide(s as unknown as StorySlideDTO))
|
|
775
|
+
)
|
|
776
|
+
|
|
777
|
+
const finalSlides = filters?.product_id
|
|
778
|
+
? hydratedSlides.filter((s) =>
|
|
779
|
+
(s.hotspots ?? []).some((h) => h.product_id === filters.product_id)
|
|
780
|
+
)
|
|
781
|
+
: hydratedSlides
|
|
782
|
+
|
|
783
|
+
const slidesByGroup = new Map<string, StorySlideDTO[]>()
|
|
784
|
+
for (const slide of finalSlides) {
|
|
785
|
+
if (slide.group_id) {
|
|
786
|
+
const list = slidesByGroup.get(slide.group_id) ?? []
|
|
787
|
+
list.push(slide)
|
|
788
|
+
slidesByGroup.set(slide.group_id, list)
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
const sortedGroups = [...rawGroups]
|
|
793
|
+
.toSorted((a, b) => (a.display_order ?? 0) - (b.display_order ?? 0))
|
|
794
|
+
.map((g) => ({
|
|
795
|
+
...(g as unknown as StoryGroupDTO),
|
|
796
|
+
slides: slidesByGroup.get(g.id) ?? [],
|
|
797
|
+
}))
|
|
798
|
+
|
|
799
|
+
return {
|
|
800
|
+
groups: sortedGroups,
|
|
801
|
+
slides: finalSlides,
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// ─── Engagement Tracking & Conversion Funnel Analytics ─────────────────────
|
|
806
|
+
|
|
807
|
+
async recordEngagementEvent(input: {
|
|
808
|
+
story_id: string
|
|
809
|
+
event_type: StoryEngagementEventType
|
|
810
|
+
hotspot_id?: string | null
|
|
811
|
+
sticker_id?: string | null
|
|
812
|
+
product_id?: string | null
|
|
813
|
+
session_id?: string | null
|
|
814
|
+
metadata?: Record<string, unknown> | null
|
|
815
|
+
}): Promise<{ ok: boolean; story: StorySlideDTO }> {
|
|
816
|
+
const existing = await this.listStories({ id: input.story_id })
|
|
817
|
+
const slide = existing?.[0]
|
|
818
|
+
if (!slide) {
|
|
819
|
+
throw new MedusaError(
|
|
820
|
+
MedusaError.Types.NOT_FOUND,
|
|
821
|
+
`Story "${input.story_id}" not found.`
|
|
822
|
+
)
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
await this.createStoryEvents({
|
|
826
|
+
story_id: slide.id,
|
|
827
|
+
group_id: slide.group_id ?? null,
|
|
828
|
+
event_type: input.event_type,
|
|
829
|
+
hotspot_id: input.hotspot_id ?? null,
|
|
830
|
+
sticker_id: input.sticker_id ?? null,
|
|
831
|
+
product_id: input.product_id ?? null,
|
|
832
|
+
session_id: input.session_id ?? null,
|
|
833
|
+
metadata: input.metadata ?? null,
|
|
834
|
+
})
|
|
835
|
+
|
|
836
|
+
const counterPatch: Record<string, unknown> = { id: slide.id }
|
|
837
|
+
if (input.event_type === "impression") {
|
|
838
|
+
counterPatch.views_count = Number(slide.views_count ?? 0) + 1
|
|
839
|
+
} else if (input.event_type === "complete") {
|
|
840
|
+
counterPatch.completions_count = Number(slide.completions_count ?? 0) + 1
|
|
841
|
+
} else if (
|
|
842
|
+
input.event_type === "hotspot_click" ||
|
|
843
|
+
input.event_type === "link_click"
|
|
844
|
+
) {
|
|
845
|
+
counterPatch.clicks_count = Number(slide.clicks_count ?? 0) + 1
|
|
846
|
+
} else if (input.event_type === "add_to_cart") {
|
|
847
|
+
counterPatch.cart_conversions_count =
|
|
848
|
+
Number(slide.cart_conversions_count ?? 0) + 1
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
const updated =
|
|
852
|
+
Object.keys(counterPatch).length > 1
|
|
853
|
+
? await this.updateStories(counterPatch)
|
|
854
|
+
: slide
|
|
855
|
+
|
|
856
|
+
return {
|
|
857
|
+
ok: true,
|
|
858
|
+
story: await this.hydrateSlide(updated as unknown as StorySlideDTO),
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
async getAnalyticsOverview(): Promise<StoriesAnalyticsOverview> {
|
|
863
|
+
const [groups, slides, stickers, events] = await Promise.all([
|
|
864
|
+
this.listStoryGroups({}),
|
|
865
|
+
this.listStories({}),
|
|
866
|
+
this.listStoryStickers({}),
|
|
867
|
+
this.listStoryEvents({}),
|
|
868
|
+
])
|
|
869
|
+
|
|
870
|
+
const now = new Date()
|
|
871
|
+
const activeSlidesCount = slides.filter(
|
|
872
|
+
(s) =>
|
|
873
|
+
s.is_active &&
|
|
874
|
+
(!s.starts_at || new Date(s.starts_at) <= now) &&
|
|
875
|
+
(!s.expires_at || new Date(s.expires_at) > now)
|
|
876
|
+
).length
|
|
877
|
+
|
|
878
|
+
const groupMap = new Map<string, { title: string }>()
|
|
879
|
+
for (const g of groups) {
|
|
880
|
+
groupMap.set(g.id, { title: g.title })
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
const stickerInteractionsByStory = new Map<string, number>()
|
|
884
|
+
let totalStickerInteractions = 0
|
|
885
|
+
for (const stk of stickers) {
|
|
886
|
+
const count = Number(stk.total_interactions ?? 0)
|
|
887
|
+
totalStickerInteractions += count
|
|
888
|
+
stickerInteractionsByStory.set(
|
|
889
|
+
stk.story_id,
|
|
890
|
+
(stickerInteractionsByStory.get(stk.story_id) ?? 0) + count
|
|
891
|
+
)
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
const linkClicksByStory = new Map<string, number>()
|
|
895
|
+
let totalLinkClicks = 0
|
|
896
|
+
for (const evt of events) {
|
|
897
|
+
if (evt.event_type === "link_click") {
|
|
898
|
+
totalLinkClicks++
|
|
899
|
+
linkClicksByStory.set(
|
|
900
|
+
evt.story_id,
|
|
901
|
+
(linkClicksByStory.get(evt.story_id) ?? 0) + 1
|
|
902
|
+
)
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
let totalImpressions = 0
|
|
907
|
+
let totalCompletions = 0
|
|
908
|
+
let totalHotspotClicks = 0
|
|
909
|
+
let totalCartConversions = 0
|
|
910
|
+
|
|
911
|
+
for (const s of slides) {
|
|
912
|
+
totalImpressions += Number(s.views_count ?? 0)
|
|
913
|
+
totalCompletions += Number(s.completions_count ?? 0)
|
|
914
|
+
totalHotspotClicks += Number(s.clicks_count ?? 0)
|
|
915
|
+
totalCartConversions += Number(s.cart_conversions_count ?? 0)
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
const byGroup: StoryGroupAnalyticsSummary[] = groups.map((g) => {
|
|
919
|
+
const groupSlides = slides.filter((s) => s.group_id === g.id)
|
|
920
|
+
const impressions = groupSlides.reduce(
|
|
921
|
+
(acc, s) => acc + Number(s.views_count ?? 0),
|
|
922
|
+
0
|
|
923
|
+
)
|
|
924
|
+
const completions = groupSlides.reduce(
|
|
925
|
+
(acc, s) => acc + Number(s.completions_count ?? 0),
|
|
926
|
+
0
|
|
927
|
+
)
|
|
928
|
+
const hotspotClicks = groupSlides.reduce(
|
|
929
|
+
(acc, s) => acc + Number(s.clicks_count ?? 0),
|
|
930
|
+
0
|
|
931
|
+
)
|
|
932
|
+
const linkClicks = groupSlides.reduce(
|
|
933
|
+
(acc, s) => acc + (linkClicksByStory.get(s.id) ?? 0),
|
|
934
|
+
0
|
|
935
|
+
)
|
|
936
|
+
const cartConversions = groupSlides.reduce(
|
|
937
|
+
(acc, s) => acc + Number(s.cart_conversions_count ?? 0),
|
|
938
|
+
0
|
|
939
|
+
)
|
|
940
|
+
const stickerInteractions = groupSlides.reduce(
|
|
941
|
+
(acc, s) => acc + (stickerInteractionsByStory.get(s.id) ?? 0),
|
|
942
|
+
0
|
|
943
|
+
)
|
|
944
|
+
|
|
945
|
+
return {
|
|
946
|
+
group_id: g.id,
|
|
947
|
+
group_title: g.title,
|
|
948
|
+
slides_count: groupSlides.length,
|
|
949
|
+
impressions,
|
|
950
|
+
completions,
|
|
951
|
+
completion_rate_pct:
|
|
952
|
+
impressions > 0 ? Math.round((completions / impressions) * 100) : 0,
|
|
953
|
+
hotspot_clicks: hotspotClicks,
|
|
954
|
+
link_clicks: linkClicks,
|
|
955
|
+
cart_conversions: cartConversions,
|
|
956
|
+
conversion_rate_pct:
|
|
957
|
+
impressions > 0
|
|
958
|
+
? Math.round((cartConversions / impressions) * 1000) / 10
|
|
959
|
+
: 0,
|
|
960
|
+
sticker_interactions: stickerInteractions,
|
|
961
|
+
}
|
|
962
|
+
})
|
|
963
|
+
|
|
964
|
+
const topSlides = [...slides]
|
|
965
|
+
.toSorted(
|
|
966
|
+
(a, b) => Number(b.views_count ?? 0) - Number(a.views_count ?? 0)
|
|
967
|
+
)
|
|
968
|
+
.slice(0, 10)
|
|
969
|
+
.map((s) => ({
|
|
970
|
+
id: s.id,
|
|
971
|
+
title: s.title,
|
|
972
|
+
group_title: s.group_id
|
|
973
|
+
? (groupMap.get(s.group_id)?.title ?? null)
|
|
974
|
+
: null,
|
|
975
|
+
impressions: Number(s.views_count ?? 0),
|
|
976
|
+
completions: Number(s.completions_count ?? 0),
|
|
977
|
+
hotspot_clicks: Number(s.clicks_count ?? 0),
|
|
978
|
+
cart_conversions: Number(s.cart_conversions_count ?? 0),
|
|
979
|
+
}))
|
|
980
|
+
|
|
981
|
+
return {
|
|
982
|
+
total_groups: groups.length,
|
|
983
|
+
active_groups: groups.filter((g) => g.is_active).length,
|
|
984
|
+
total_slides: slides.length,
|
|
985
|
+
active_slides: activeSlidesCount,
|
|
986
|
+
total_impressions: totalImpressions,
|
|
987
|
+
total_completions: totalCompletions,
|
|
988
|
+
overall_completion_rate_pct:
|
|
989
|
+
totalImpressions > 0
|
|
990
|
+
? Math.round((totalCompletions / totalImpressions) * 100)
|
|
991
|
+
: 0,
|
|
992
|
+
total_hotspot_clicks: totalHotspotClicks,
|
|
993
|
+
total_link_clicks: totalLinkClicks,
|
|
994
|
+
total_cart_conversions: totalCartConversions,
|
|
995
|
+
overall_conversion_rate_pct:
|
|
996
|
+
totalImpressions > 0
|
|
997
|
+
? Math.round((totalCartConversions / totalImpressions) * 1000) / 10
|
|
998
|
+
: 0,
|
|
999
|
+
total_sticker_interactions: totalStickerInteractions,
|
|
1000
|
+
by_group: byGroup,
|
|
1001
|
+
top_slides: topSlides,
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
// ─── Scheduled Expiration & Lifecycle Management ───────────────────────────
|
|
1006
|
+
|
|
1007
|
+
async expireOutdatedStories(referenceTime?: Date): Promise<{
|
|
1008
|
+
expired_slides: number
|
|
1009
|
+
expired_ids: string[]
|
|
1010
|
+
}> {
|
|
1011
|
+
const now = referenceTime ?? new Date()
|
|
1012
|
+
const activeSlides = await this.listStories({ is_active: true })
|
|
1013
|
+
const expiredIds: string[] = []
|
|
1014
|
+
|
|
1015
|
+
for (const slide of activeSlides) {
|
|
1016
|
+
if (slide.expires_at && new Date(slide.expires_at) <= now) {
|
|
1017
|
+
await this.updateStories({
|
|
1018
|
+
id: slide.id,
|
|
1019
|
+
is_active: false,
|
|
1020
|
+
})
|
|
1021
|
+
expiredIds.push(slide.id)
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
return {
|
|
1026
|
+
expired_slides: expiredIds.length,
|
|
1027
|
+
expired_ids: expiredIds,
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
async removeHotspotsByProductId(productId: string): Promise<{
|
|
1032
|
+
removed_count: number
|
|
1033
|
+
removed_ids: string[]
|
|
1034
|
+
}> {
|
|
1035
|
+
const trimmed = String(productId || "").trim()
|
|
1036
|
+
if (!trimmed) {
|
|
1037
|
+
return { removed_count: 0, removed_ids: [] }
|
|
1038
|
+
}
|
|
1039
|
+
const matching = await this.listStoryHotspots({ product_id: trimmed })
|
|
1040
|
+
const removedIds: string[] = []
|
|
1041
|
+
for (const hs of matching ?? []) {
|
|
1042
|
+
await this.deleteStoryHotspots(hs.id)
|
|
1043
|
+
removedIds.push(hs.id)
|
|
1044
|
+
}
|
|
1045
|
+
return {
|
|
1046
|
+
removed_count: removedIds.length,
|
|
1047
|
+
removed_ids: removedIds,
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
export default StoriesModuleService
|