overleaf/services/web/scripts/confirmed_at_to_dates.js
nate stemen 7e61fc4035 Merge pull request #4330 from overleaf/ns-update-confirmedAt_strings
migrate confirmedAt strings to date types

GitOrigin-RevId: 5e8bde9b52b6696ed89a34ce58f00f31a921a57a
2021-07-28 02:07:11 +00:00

54 lines
1.2 KiB
JavaScript

const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
async function updateStringDates() {
await waitForDb()
const users = await db.users.aggregate([
{ $unwind: { path: '$emails' } },
{
$match: { 'emails.confirmedAt': { $exists: true, $type: 'string' } },
},
{
$project: {
_id: 1,
'emails.email': 1,
'emails.confirmedAt': 1,
},
},
])
let user
let count = 0
while ((user = await users.next())) {
count += 1
if (count % 10000 === 0) {
console.log(`processed ${count} users`)
}
const confirmedAt = user.emails.confirmedAt
const dateConfirmedAt = new Date(confirmedAt.replace(/ UTC$/, ''))
await db.users.updateOne(
{
_id: user._id,
'emails.email': user.emails.email,
},
{
$set: {
'emails.$.confirmedAt': dateConfirmedAt,
},
}
)
}
console.log(`Updated ${count} confirmedAt strings to dates!`)
}
if (!module.parent) {
updateStringDates()
.then(() => {
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
})
}
module.exports = updateStringDates