2024-10-18 07:04:57 -04:00
|
|
|
import { ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js'
|
|
|
|
import UserUpdater from '../app/src/Features/User/UserUpdater.js'
|
|
|
|
import UserGetter from '../app/src/Features/User/UserGetter.js'
|
2022-01-12 11:19:30 -05:00
|
|
|
|
|
|
|
async function removeEmail() {
|
|
|
|
const userId = process.argv[2]
|
|
|
|
let email = process.argv[3]
|
|
|
|
|
|
|
|
if (!ObjectId.isValid(userId)) {
|
|
|
|
throw new Error(`user ID ${userId} is not valid`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!email) {
|
|
|
|
throw new Error('no email provided')
|
|
|
|
}
|
|
|
|
|
|
|
|
// email arg can be within double quotes for arg so that we can handle
|
|
|
|
// malformed emails with spaces
|
|
|
|
email = email.replace(/"/g, '')
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
`\nBegin request to remove email "${email}" from user "${userId}"\n`
|
|
|
|
)
|
|
|
|
|
|
|
|
const userWithEmail = await UserGetter.promises.getUserByAnyEmail(email, {
|
|
|
|
_id: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!userWithEmail) {
|
|
|
|
throw new Error(`no user found with email "${email}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userWithEmail._id.toString() !== userId) {
|
|
|
|
throw new Error(
|
|
|
|
`email does not belong to user. Belongs to ${userWithEmail._id}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-27 04:25:50 -04:00
|
|
|
const auditLog = {
|
|
|
|
initiatorId: undefined,
|
|
|
|
ipAddress: '0.0.0.0',
|
|
|
|
extraInfo: {
|
|
|
|
script: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-12 11:19:30 -05:00
|
|
|
const skipParseEmail = true
|
2022-07-27 04:25:50 -04:00
|
|
|
await UserUpdater.promises.removeEmailAddress(
|
|
|
|
userId,
|
|
|
|
email,
|
|
|
|
auditLog,
|
|
|
|
skipParseEmail
|
|
|
|
)
|
2022-01-12 11:19:30 -05:00
|
|
|
}
|
2024-10-18 07:04:57 -04:00
|
|
|
try {
|
|
|
|
await waitForDb()
|
|
|
|
await removeEmail()
|
|
|
|
console.log('Done.')
|
|
|
|
process.exit()
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
}
|