Spaces:
Running
Running
File size: 21,821 Bytes
a733514 ff790f0 a733514 ff790f0 a733514 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { stat, writeFile } from 'fs/promises';
import path from 'path';
import { createClient, type SupabaseClient } from '@supabase/supabase-js';
import type {
PublicReviewCampaign,
PublicSubmitResult,
ReviewRewardType,
ReviewSubmissionStatus,
} from '@/lib/reviews/types';
/**
* Review store for the standalone prototype.
*
* Preferred mode: Supabase Storage
* review-videos/videos/<submissionId>.<ext>
* review-videos/submissions/<submissionId>.json
*
* Local file fallback remains available when Supabase env vars are absent.
*/
export type LocalSubmission = {
submissionId: string;
interviewId: string;
campaignSlug: string;
status: ReviewSubmissionStatus;
decision: 'pass' | 'fail_and_retry' | null;
feedback: string;
reward: { type: ReviewRewardType; value: string } | null;
reasons: string[];
consentAccepted: boolean;
socialHandle: string | null;
deviceKey: string | null;
tableId: string | null;
durationSeconds: number;
videoSize: number;
videoMime: string;
videoFileName: string;
storagePath: string;
storageBackend: 'local' | 'supabase';
createdAt: number;
updatedAt: string;
};
export type AdminReviewSubmission = LocalSubmission & {
restaurantName: string;
previewUrl: string;
createdAtIso: string;
};
type StoreState = {
campaigns: Map<string, PublicReviewCampaign>;
submissions: Map<string, LocalSubmission>;
};
type LocalVideoResult = {
kind: 'local';
submission: LocalSubmission;
filePath: string;
size: number;
contentType: string;
};
type SupabaseVideoResult = {
kind: 'supabase';
submission: LocalSubmission;
storagePath: string;
size: number;
contentType: string;
};
export type SubmissionVideoResult = LocalVideoResult | SupabaseVideoResult;
const LOCAL_DATA_DIR = path.join(process.cwd(), '.local-review-data');
const UPLOADS_DIR = path.join(LOCAL_DATA_DIR, 'uploads');
const SUBMISSIONS_FILE = path.join(LOCAL_DATA_DIR, 'submissions.json');
const ENV_FILE = path.join(process.cwd(), '.env.local');
const DEFAULT_BUCKET = 'review-videos';
const SAGE_AND_STONE: PublicReviewCampaign = {
id: 'sage-and-stone-cafe',
slug: 'sageandstone',
restaurantName: 'Sage & Stone Cafe',
status: 'active',
rulesConfig: {
minDurationSeconds: 8,
maxDurationSeconds: 90,
minWordCount: 8,
requireRestaurantMention: false,
blockedTerms: [],
},
settings: { dailyRewardLimitPerDevice: 1 },
mode: 'guided_clips',
prompts: [
{
step: 1,
title: 'Close-up pan of the food.',
tip: 'Move slowly across the texture, sauce, steam, and toppings.',
mediaType: 'video',
camera: 'rear',
maxSeconds: 10,
optional: false,
},
{
step: 2,
title: 'Wide shot of the table.',
tip: 'Show the full plate, drink, table setup, and a little cafe vibe.',
mediaType: 'video',
camera: 'rear',
maxSeconds: 10,
optional: false,
},
{
step: 3,
title: 'Action detail of the food.',
tip: 'Cut, scoop, pour, lift, or show the best bite without focusing on your face.',
mediaType: 'video',
camera: 'rear',
maxSeconds: 10,
optional: false,
},
{
step: 4,
title: 'What did you order?',
tip: 'Voice only. Say the dish name and describe what is on the plate.',
mediaType: 'audio',
camera: 'front',
maxSeconds: 7,
optional: false,
},
{
step: 5,
title: 'What did you like about it?',
tip: 'Voice only. Mention the flavor, texture, portion, or what stood out.',
mediaType: 'audio',
camera: 'front',
maxSeconds: 10,
optional: false,
},
{
step: 6,
title: 'Optional: quick reaction shot.',
tip: 'Take one bite or sip and react naturally. Skip if you would rather keep it food-only.',
mediaType: 'video',
camera: 'front',
maxSeconds: 4,
optional: true,
},
],
rewardType: 'static_code',
rewardValue: null,
theme: 'cafe-cream',
};
let supabaseAdmin: SupabaseClient | null = null;
let bucketReady = false;
let envFileCache: Record<string, string> | null = null;
function readEnvFile() {
if (envFileCache) return envFileCache;
envFileCache = {};
if (!existsSync(ENV_FILE)) return envFileCache;
try {
const raw = readFileSync(ENV_FILE, 'utf8');
for (const line of raw.split(/\r?\n/)) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const splitAt = trimmed.indexOf('=');
if (splitAt <= 0) continue;
const key = trimmed.slice(0, splitAt).trim();
const value = trimmed.slice(splitAt + 1).trim();
envFileCache[key] = value.replace(/^['"]|['"]$/g, '');
}
} catch {
envFileCache = {};
}
return envFileCache;
}
function serverEnv(name: string) {
return process.env[name] || readEnvFile()[name] || '';
}
function hasSupabaseConfig() {
return Boolean(serverEnv('SUPABASE_URL') && serverEnv('SUPABASE_SERVICE_ROLE_KEY'));
}
function getBucketName() {
return serverEnv('SUPABASE_REVIEW_VIDEO_BUCKET') || DEFAULT_BUCKET;
}
function getSupabaseAdmin() {
if (!hasSupabaseConfig()) return null;
if (!supabaseAdmin) {
supabaseAdmin = createClient(serverEnv('SUPABASE_URL'), serverEnv('SUPABASE_SERVICE_ROLE_KEY'), {
auth: {
autoRefreshToken: false,
persistSession: false,
},
global: {
fetch: (input, init) => fetch(input, { ...init, cache: 'no-store' }),
},
});
}
return supabaseAdmin;
}
function supabaseObjectUrl(storagePath: string) {
const baseUrl = serverEnv('SUPABASE_URL').replace(/\/$/, '');
const encodedBucket = encodeURIComponent(getBucketName());
const encodedPath = storagePath.split('/').map(encodeURIComponent).join('/');
return `${baseUrl}/storage/v1/object/${encodedBucket}/${encodedPath}`;
}
async function ensureSupabaseBucket(client: SupabaseClient) {
if (bucketReady) return;
const bucket = getBucketName();
const { data } = await client.storage.getBucket(bucket);
if (!data) {
const { error } = await client.storage.createBucket(bucket, {
public: false,
});
if (error && !error.message.toLowerCase().includes('already exists')) {
throw error;
}
}
bucketReady = true;
}
function ensureLocalDirs() {
mkdirSync(UPLOADS_DIR, { recursive: true });
}
function readPersistedSubmissions() {
if (!existsSync(SUBMISSIONS_FILE)) return [];
try {
const raw = readFileSync(SUBMISSIONS_FILE, 'utf8');
const parsed = JSON.parse(raw) as unknown;
if (!Array.isArray(parsed)) return [];
return parsed.filter(isLocalSubmission);
} catch {
return [];
}
}
function isLocalSubmission(value: unknown): value is LocalSubmission {
if (!value || typeof value !== 'object') return false;
const maybe = value as Partial<LocalSubmission>;
return (
typeof maybe.submissionId === 'string' &&
typeof maybe.interviewId === 'string' &&
typeof maybe.campaignSlug === 'string' &&
typeof maybe.status === 'string' &&
typeof maybe.storagePath === 'string' &&
typeof maybe.createdAt === 'number'
);
}
function normalizeSubmission(value: LocalSubmission): LocalSubmission {
return {
...value,
storageBackend: value.storageBackend ?? 'local',
};
}
function createState(): StoreState {
const campaigns = new Map<string, PublicReviewCampaign>();
campaigns.set(SAGE_AND_STONE.slug, SAGE_AND_STONE);
const submissions = new Map<string, LocalSubmission>();
for (const submission of readPersistedSubmissions()) {
const normalized = normalizeSubmission(submission);
submissions.set(normalized.submissionId, normalized);
}
return { campaigns, submissions };
}
const globalForStore = globalThis as unknown as {
__matchaReviewStore?: StoreState;
};
const state = globalForStore.__matchaReviewStore ?? createState();
state.campaigns.set(SAGE_AND_STONE.slug, SAGE_AND_STONE);
for (const [id, submission] of state.submissions.entries()) {
if (!isLocalSubmission(submission)) {
state.submissions.delete(id);
} else {
state.submissions.set(id, normalizeSubmission(submission));
}
}
if (process.env.NODE_ENV !== 'production') {
globalForStore.__matchaReviewStore = state;
}
function persistLocalSubmissions() {
ensureLocalDirs();
const submissions = [...state.submissions.values()]
.filter((submission) => submission.storageBackend === 'local')
.sort((a, b) => a.createdAt - b.createdAt);
writeFileSync(SUBMISSIONS_FILE, `${JSON.stringify(submissions, null, 2)}\n`, 'utf8');
}
export function getCampaignBySlug(slug: string): PublicReviewCampaign | null {
return state.campaigns.get(slug) ?? null;
}
function newId(prefix: string) {
const rand = Math.random().toString(36).slice(2, 10);
return `${prefix}_${Date.now().toString(36)}_${rand}`;
}
function rewardCode() {
return `MATCHA-${Math.random().toString(36).slice(2, 6).toUpperCase()}`;
}
function resolveVideoExt(fileName: string, mime: string) {
const lowerName = fileName.toLowerCase();
const lowerMime = mime.toLowerCase();
if (lowerName.endsWith('.mp4') || lowerMime.includes('mp4')) return 'mp4';
if (lowerName.endsWith('.mov') || lowerMime.includes('quicktime')) return 'mov';
if (lowerName.endsWith('.webm') || lowerMime.includes('webm')) return 'webm';
return 'webm';
}
function buildPreviewUrl(submissionId: string) {
return `/api/public/reviews/video/${encodeURIComponent(submissionId)}`;
}
function localStoragePathFor(submissionId: string, ext: string) {
return `uploads/${submissionId}.${ext}`;
}
function supabaseVideoPathFor(submissionId: string, ext: string) {
return `videos/${submissionId}.${ext}`;
}
function metadataPathFor(submissionId: string) {
return `submissions/${submissionId}.json`;
}
function absoluteStoragePath(storagePath: string) {
const absolute = path.resolve(LOCAL_DATA_DIR, storagePath);
const localRoot = path.resolve(LOCAL_DATA_DIR);
if (absolute !== localRoot && !absolute.startsWith(`${localRoot}${path.sep}`)) {
return null;
}
return absolute;
}
async function textFromDownloadedData(data: unknown) {
if (typeof data === 'string') return data;
if (data && typeof data === 'object') {
const maybeBlob = data as {
text?: () => Promise<string>;
arrayBuffer?: () => Promise<ArrayBuffer>;
};
if (typeof maybeBlob.text === 'function') {
return maybeBlob.text();
}
if (typeof maybeBlob.arrayBuffer === 'function') {
return Buffer.from(await maybeBlob.arrayBuffer()).toString('utf8');
}
}
if (data instanceof ArrayBuffer) return Buffer.from(data).toString('utf8');
if (Buffer.isBuffer(data)) return data.toString('utf8');
throw new Error('Unsupported downloaded data type');
}
async function persistSupabaseSubmission(client: SupabaseClient, submission: LocalSubmission) {
await ensureSupabaseBucket(client);
const body = JSON.stringify(submission, null, 2);
const { error } = await client.storage
.from(getBucketName())
.upload(metadataPathFor(submission.submissionId), body, {
contentType: 'application/json',
upsert: true,
});
if (error) throw error;
}
async function loadSupabaseSubmission(
client: SupabaseClient,
submissionId: string,
): Promise<LocalSubmission | null> {
await ensureSupabaseBucket(client);
const { data, error } = await client.storage
.from(getBucketName())
.download(metadataPathFor(submissionId));
if (error || !data) {
if (error) {
console.warn('[matcha-moments/review-store] failed to download Supabase metadata', {
submissionId,
message: error.message,
});
}
return null;
}
try {
const text = await textFromDownloadedData(data);
const parsed = JSON.parse(text) as unknown;
if (!isLocalSubmission(parsed)) {
console.warn('[matcha-moments/review-store] invalid Supabase metadata', {
submissionId,
});
return null;
}
return normalizeSubmission(parsed);
} catch (err) {
console.warn('[matcha-moments/review-store] failed to parse Supabase metadata', {
submissionId,
message: err instanceof Error ? err.message : 'Unknown error',
});
return null;
}
}
async function listSupabaseSubmissions(client: SupabaseClient) {
await ensureSupabaseBucket(client);
const { data, error } = await client.storage
.from(getBucketName())
.list('submissions', {
limit: 200,
sortBy: { column: 'created_at', order: 'desc' },
});
if (error || !data) {
if (error) {
console.warn('[matcha-moments/review-store] failed to list Supabase submissions', error);
}
return [];
}
const submissions = await Promise.all(
data
.filter((item) => item.name.endsWith('.json'))
.map((item) => loadSupabaseSubmission(client, item.name.replace(/\.json$/, ''))),
);
return submissions.filter((submission): submission is LocalSubmission => Boolean(submission));
}
function mergeSubmissions(...groups: LocalSubmission[][]) {
const merged = new Map<string, LocalSubmission>();
for (const group of groups) {
for (const submission of group) {
merged.set(submission.submissionId, normalizeSubmission(submission));
}
}
return [...merged.values()];
}
async function persistSubmission(submission: LocalSubmission) {
state.submissions.set(submission.submissionId, submission);
if (submission.storageBackend === 'supabase') {
const client = getSupabaseAdmin();
if (!client) throw new Error('Supabase is not configured');
await persistSupabaseSubmission(client, submission);
return;
}
persistLocalSubmissions();
}
function advanceSubmission(submission: LocalSubmission) {
if (
submission.status === 'reward_issued' ||
submission.status === 'processing_failed' ||
submission.status === 'fail_and_retry'
) {
return false;
}
const campaign = getCampaignBySlug(submission.campaignSlug);
if (!campaign) return false;
const elapsed = Date.now() - submission.createdAt;
let changed = false;
if (submission.status === 'opened' && elapsed >= 1000) {
submission.status = 'processing_interview';
changed = true;
}
if (submission.status === 'processing_interview' && elapsed >= 2500) {
submission.status = 'evaluating_rules';
changed = true;
}
if (submission.status === 'evaluating_rules' && elapsed >= 4000) {
submission.status = 'reward_issued';
submission.decision = 'pass';
submission.feedback = 'Office prototype approved. Show the matcha code to staff.';
submission.reward = {
type: campaign.rewardType,
value: campaign.rewardValue ?? rewardCode(),
};
changed = true;
}
if (changed) {
submission.updatedAt = new Date().toISOString();
}
return changed;
}
export type CreateSubmissionInput = {
slug: string;
consentAccepted: boolean;
socialHandle?: string | null;
deviceKey?: string | null;
tableId?: string | null;
durationSeconds: number;
video: File;
};
export async function createSubmission(input: CreateSubmissionInput):
Promise<
| { ok: true; submission: LocalSubmission }
| { ok: false; status: number; error: string }
> {
if (!input.slug) {
return { ok: false, status: 400, error: 'Missing campaign slug' };
}
if (!input.consentAccepted) {
return { ok: false, status: 400, error: 'Consent is required' };
}
if (!(input.video instanceof File) || input.video.size <= 0) {
return { ok: false, status: 400, error: 'A video file is required' };
}
if (!input.video.type.startsWith('video/')) {
return { ok: false, status: 400, error: 'Only video uploads are supported' };
}
const campaign = getCampaignBySlug(input.slug);
if (!campaign) {
return { ok: false, status: 404, error: 'Campaign not found' };
}
const submissionId = newId('sub');
const interviewId = newId('iv');
const videoMime = input.video.type || 'video/webm';
const videoFileName = input.video.name || 'matcha-moments.webm';
const ext = resolveVideoExt(videoFileName, videoMime);
const bytes = Buffer.from(await input.video.arrayBuffer());
const storageBackend = hasSupabaseConfig() ? 'supabase' : 'local';
const storagePath =
storageBackend === 'supabase'
? supabaseVideoPathFor(submissionId, ext)
: localStoragePathFor(submissionId, ext);
if (storageBackend === 'supabase') {
const client = getSupabaseAdmin();
if (!client) {
return { ok: false, status: 500, error: 'Supabase is not configured' };
}
await ensureSupabaseBucket(client);
const { error } = await client.storage.from(getBucketName()).upload(storagePath, bytes, {
contentType: videoMime,
upsert: false,
});
if (error) {
return { ok: false, status: 500, error: `Video upload failed: ${error.message}` };
}
} else {
const absolutePath = absoluteStoragePath(storagePath);
if (!absolutePath) {
return { ok: false, status: 500, error: 'Invalid local storage path' };
}
ensureLocalDirs();
await writeFile(absolutePath, bytes);
}
const now = new Date().toISOString();
const submission: LocalSubmission = {
submissionId,
interviewId,
campaignSlug: input.slug,
status: 'opened',
decision: null,
feedback: 'Your review is being processed.',
reward: null,
reasons: [],
consentAccepted: input.consentAccepted,
socialHandle: input.socialHandle?.trim() || null,
deviceKey: input.deviceKey?.trim() || null,
tableId: input.tableId?.trim() || null,
durationSeconds: Math.max(0, Math.round(input.durationSeconds)),
videoSize: input.video.size,
videoMime,
videoFileName,
storagePath,
storageBackend,
createdAt: Date.now(),
updatedAt: now,
};
await persistSubmission(submission);
return { ok: true, submission };
}
export async function getSubmission(
submissionId: string,
slug: string,
): Promise<
| { ok: true; submission: LocalSubmission }
| { ok: false; status: number; error: string }
> {
const campaign = getCampaignBySlug(slug);
if (!campaign) return { ok: false, status: 404, error: 'Campaign not found' };
const client = getSupabaseAdmin();
const submission =
(client ? await loadSupabaseSubmission(client, submissionId) : null) ??
state.submissions.get(submissionId);
if (!submission || submission.campaignSlug !== slug) {
return { ok: false, status: 404, error: 'Submission not found' };
}
if (advanceSubmission(submission)) {
await persistSubmission(submission);
} else {
state.submissions.set(submission.submissionId, submission);
}
return { ok: true, submission };
}
export async function listAdminReviewSubmissions(): Promise<AdminReviewSubmission[]> {
const client = getSupabaseAdmin();
const supabaseSubmissions = client ? await listSupabaseSubmissions(client) : [];
const sourceSubmissions = mergeSubmissions(
[...state.submissions.values()],
supabaseSubmissions,
);
const submissions: LocalSubmission[] = [];
for (const submission of sourceSubmissions) {
if (advanceSubmission(submission)) {
await persistSubmission(submission);
} else {
state.submissions.set(submission.submissionId, submission);
}
submissions.push(submission);
}
return submissions
.sort((a, b) => b.createdAt - a.createdAt)
.map((submission) => ({
...submission,
restaurantName: getCampaignBySlug(submission.campaignSlug)?.restaurantName ?? submission.campaignSlug,
previewUrl: buildPreviewUrl(submission.submissionId),
createdAtIso: new Date(submission.createdAt).toISOString(),
}));
}
export async function getSubmissionVideo(
submissionId: string,
): Promise<SubmissionVideoResult | null> {
const client = getSupabaseAdmin();
const submission =
(client ? await loadSupabaseSubmission(client, submissionId) : null) ??
state.submissions.get(submissionId);
if (!submission) return null;
if (submission.storageBackend === 'supabase') {
if (!client) return null;
return {
kind: 'supabase',
submission,
storagePath: submission.storagePath,
size: submission.videoSize,
contentType: submission.videoMime || 'video/webm',
};
}
const filePath = absoluteStoragePath(submission.storagePath);
if (!filePath) return null;
try {
const fileStat = await stat(filePath);
if (!fileStat.isFile()) return null;
return {
kind: 'local',
submission,
filePath,
size: fileStat.size,
contentType: submission.videoMime || 'video/webm',
};
} catch {
return null;
}
}
export async function fetchSupabaseVideoObject(
video: Extract<SubmissionVideoResult, { kind: 'supabase' }>,
rangeHeader: string | null,
) {
const serviceRoleKey = serverEnv('SUPABASE_SERVICE_ROLE_KEY');
if (!serviceRoleKey) return null;
const headers: HeadersInit = {
apikey: serviceRoleKey,
Authorization: `Bearer ${serviceRoleKey}`,
};
if (rangeHeader) {
headers.Range = rangeHeader;
}
const response = await fetch(supabaseObjectUrl(video.storagePath), {
headers,
cache: 'no-store',
});
if (!response.ok || !response.body) return null;
return response;
}
export function toPublicSubmitResult(submission: LocalSubmission): PublicSubmitResult {
return {
submissionId: submission.submissionId,
interviewId: submission.interviewId,
status: submission.status,
decision: submission.decision,
feedback: submission.feedback,
reward: submission.reward,
reasons: submission.reasons,
previewUrl: buildPreviewUrl(submission.submissionId),
updatedAt: submission.updatedAt,
};
}
|