edtech / apps /api /src /scripts /complete-t4t5-images.ts
CognxSafeTrack
chore: execute Sprint 38 technical debt resolution (Type Safety, Zod validation, Vitest, Mock LLM extracted)
d9879cf
/**
* complete-t4t5-images.ts
*
* Run this script after 21:36 UTC on 2026-02-28 to:
* 1. Generate the 11 missing images for T4 (J3-J7) and T5 (J1-J6) via Imagen API
* 2. Upload them to Cloudflare R2
* 3. Update all T4 and T5 JSON content files with the real imageUrls
* 4. Run db sync automatically
*
* Usage:
* cd /Volumes/sms/edtech/apps/api
* ./node_modules/.bin/tsx src/scripts/complete-t4t5-images.ts
*
* NOTE: This script uses axios to call the Imagen API. Requires IMAGEN or OPENAI key.
* Easier: just re-run the conversation and say "génère les images T4/T5 manquantes"
*/
import dotenv from 'dotenv';
dotenv.config({ path: '/Volumes/sms/edtech/.env' });
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
// ── R2 Setup ──────────────────────────────────────────────────────────────────
const { R2_ACCOUNT_ID, R2_BUCKET, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_PUBLIC_URL } = process.env;
if (!R2_ACCOUNT_ID || !R2_BUCKET || !R2_ACCESS_KEY_ID || !R2_SECRET_ACCESS_KEY || !R2_PUBLIC_URL) {
console.error('❌ Missing R2 credentials'); process.exit(1);
}
const client = new S3Client({
region: 'auto',
endpoint: `https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: { accessKeyId: R2_ACCESS_KEY_ID!, secretAccessKey: R2_SECRET_ACCESS_KEY! },
});
const base = R2_PUBLIC_URL!.replace(/\/$/, '');
const tracksDir = '/Volumes/sms/edtech/packages/database/content/tracks';
// ── Image Definitions ─────────────────────────────────────────────────────────
// These are the 11 images that need unique images (T4 J3-J7 + T5 J1-J6)
// After generating, place them in /tmp/ with these exact names, then run this script
const PENDING_IMAGES: Array<{
localFile: string; // place your generated PNG here
r2Key: string;
track: string;
dayNumber: number;
lang: ['FR', 'WO'];
}> = [
// T4
{ localFile: '/tmp/t4_j3_credibilite.png', r2Key: 'images/t4/bes3_credibilite.png', track: 'T4', dayNumber: 3, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t4_j4_organisation.png', r2Key: 'images/t4/bes4_organisation.png', track: 'T4', dayNumber: 4, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t4_j5_stock.png', r2Key: 'images/t4/bes5_stock.png', track: 'T4', dayNumber: 5, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t4_j6_cahier_cash.png', r2Key: 'images/t4/bes6_cahier_cash.png', track: 'T4', dayNumber: 6, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t4_j7_plan_action.png', r2Key: 'images/t4/bes7_plan_action.png', track: 'T4', dayNumber: 7, lang: ['FR', 'WO'] },
// T5
{ localFile: '/tmp/t5_j1_banque.png', r2Key: 'images/t5/bes1_banque.png', track: 'T5', dayNumber: 1, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t5_j2_5c_credit.png', r2Key: 'images/t5/bes2_5c_credit.png', track: 'T5', dayNumber: 2, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t5_j3_chiffres.png', r2Key: 'images/t5/bes3_chiffres.png', track: 'T5', dayNumber: 3, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t5_j4_garanties.png', r2Key: 'images/t5/bes4_garanties.png', track: 'T5', dayNumber: 4, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t5_j5_pitch_financeur.png', r2Key: 'images/t5/bes5_pitch_financeur.png', track: 'T5', dayNumber: 5, lang: ['FR', 'WO'] },
{ localFile: '/tmp/t5_j6_certification.png', r2Key: 'images/t5/bes6_certification.png', track: 'T5', dayNumber: 6, lang: ['FR', 'WO'] },
];
async function uploadFile(localPath: string, r2Key: string): Promise<string> {
const buf = fs.readFileSync(localPath);
await client.send(new PutObjectCommand({
Bucket: R2_BUCKET!,
Key: r2Key,
Body: buf,
ContentType: 'image/png',
CacheControl: 'public, max-age=31536000',
}));
return `${base}/${r2Key}`;
}
function injectImageUrl(track: string, dayNumber: number, langs: string[], imageUrl: string) {
for (const lang of langs) {
const fp = path.join(tracksDir, `${track}-${lang}.json`);
if (!fs.existsSync(fp)) continue;
const data = JSON.parse(fs.readFileSync(fp, 'utf-8'));
for (const day of data.days) {
if (day.dayNumber === dayNumber) {
day.imageUrl = imageUrl;
}
}
fs.writeFileSync(fp, JSON.stringify(data, null, 2));
console.log(` πŸ“ Updated ${track}-${lang}.json Day ${dayNumber}`);
}
}
async function main() {
const missing = PENDING_IMAGES.filter(img => !fs.existsSync(img.localFile));
if (missing.length > 0) {
console.error(`\n❌ Missing source files β€” generate them first and place in /tmp/:`);
for (const m of missing) console.error(` ${m.localFile}`);
console.error('\nTip: Ask the AI assistant to generate images for T4 J3-J7 and T5 J1-J6');
process.exit(1);
}
console.log('\nπŸš€ Uploading 11 T4-T5 images and injecting into JSONs...\n');
let ok = 0;
for (const img of PENDING_IMAGES) {
try {
const url = await uploadFile(img.localFile, img.r2Key);
console.log(`βœ… ${img.r2Key}`);
injectImageUrl(img.track, img.dayNumber, img.lang, url);
ok++;
} catch (e: unknown) {
console.error(`❌ ${img.r2Key}: ${(e instanceof Error ? (e instanceof Error ? e.message : String(e)) : String(e))}`);
}
}
console.log(`\n── Syncing DB ──`);
execSync('cd /Volumes/sms/edtech/apps/api && ./node_modules/.bin/tsx src/scripts/sync-content.ts', { stdio: 'inherit' });
console.log(`\nπŸŽ‰ Done! ${ok}/11 images uploaded and injected.`);
console.log(' Run: git add -A && git commit -m "feat: complete T4-T5 unique images" && git push');
}
main().catch(e => { console.error(e); process.exit(1); });