import mongoose from 'mongoose'; import { env } from '../src/config/env.js'; import { OnboardedPatient } from '../src/models/OnboardedPatient.js'; import { User } from '../src/models/User.js'; import { applyOnboardedPii, encryptSecret, isCipherPayload } from '../src/services/cryptoPii.js'; await mongoose.connect(env.MONGODB_URI); let patients = 0; const items = await OnboardedPatient.find(); for (const item of items) { if (isCipherPayload(item.name) && item.mobileHash) continue; applyOnboardedPii(item, { name: typeof item.name === 'string' ? item.name : item.name, mobile: typeof item.mobile === 'string' ? item.mobile : item.mobile, age: item.age, gender: item.gender, lungCondition: item.lungCondition, otherHealthCondition: item.otherHealthCondition, }); await item.save(); patients += 1; } let tokens = 0; const users = await User.find({ $or: [{ 'google.accessToken': { $ne: '' } }, { 'google.refreshToken': { $ne: '' } }] }); for (const user of users) { let dirty = false; if (user.google?.accessToken && !isCipherPayload(user.google.accessToken)) { user.google.accessToken = encryptSecret(user.google.accessToken); dirty = true; } if (user.google?.refreshToken && !isCipherPayload(user.google.refreshToken)) { user.google.refreshToken = encryptSecret(user.google.refreshToken); dirty = true; } if (dirty) { await user.save(); tokens += 1; } } console.log(`Encrypted ${patients} onboarded patient(s) and ${tokens} Google token set(s).`); await mongoose.disconnect();