34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
/**
|
|
* One-off: set Session.joinCode for all existing sessions (same algorithm as dashboard eventBuzzCode).
|
|
* Run: cd backend && node scripts/backfill-session-join-codes.js
|
|
*/
|
|
const path = require('path');
|
|
|
|
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
|
|
const mongoose = require('mongoose');
|
|
const Session = require(path.join(__dirname, '..', 'src', 'models', 'Session'));
|
|
const { eventBuzzCodeFor } = require(path.join(__dirname, '..', 'src', 'utils', 'eventBuzzCode'));
|
|
|
|
async function run() {
|
|
const uri = process.env.MONGODB_URI;
|
|
if (!uri) {
|
|
console.error('MONGODB_URI is not set');
|
|
process.exit(1);
|
|
}
|
|
await mongoose.connect(uri);
|
|
const sessions = await Session.find({}).select('_id').lean();
|
|
let n = 0;
|
|
for (const s of sessions) {
|
|
const jc = eventBuzzCodeFor(s._id.toString(), 'session');
|
|
await Session.updateOne({ _id: s._id }, { $set: { joinCode: jc } });
|
|
n += 1;
|
|
}
|
|
console.log(`Backfilled joinCode for ${n} session(s).`);
|
|
await mongoose.disconnect();
|
|
}
|
|
|
|
run().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|