feat: update migration to populate user avatar attachments

This commit is contained in:
Mohamed Marrouchi 2025-01-08 19:57:42 +01:00
parent 9e96055a55
commit 65fad2f9e6

View File

@ -44,8 +44,83 @@ const getAdminUser = async () => {
}; };
/** /**
* Updates subscriber documents with their corresponding avatar attachments * Updates user attachment documents to populate new attributes (context, owner, ownerType)
* and moves avatar files to a new directory. *
* @returns Resolves when the migration process is complete.
*/
const populateUserAvatar = async ({ logger }: MigrationServices) => {
const AttachmentModel = mongoose.model<Attachment>(
Attachment.name,
attachmentSchema,
);
const UserModel = mongoose.model<User>(User.name, userSchema);
const cursor = UserModel.find({
avatar: { $exists: true, $ne: null },
}).cursor();
for await (const user of cursor) {
try {
await AttachmentModel.updateOne(
{ _id: user.avatar },
{
$set: {
context: AttachmentContext.UserAvatar,
ownerType: AttachmentOwnerType.User,
owner: user._id,
},
},
);
logger.log(`User ${user._id} avatar attributes successfully populated`);
} catch (error) {
logger.error(
`Failed to populate avatar attributes for user ${user._id}: ${error.message}`,
);
}
}
};
/**
* Reverts what the previous function does
*
* @returns Resolves when the migration process is complete.
*/
const unpopulateUserAvatar = async ({ logger }: MigrationServices) => {
const AttachmentModel = mongoose.model<Attachment>(
Attachment.name,
attachmentSchema,
);
const UserModel = mongoose.model<User>(User.name, userSchema);
const cursor = UserModel.find({
avatar: { $exists: true, $ne: null },
}).cursor();
for await (const user of cursor) {
try {
// Undo the updates made by populateUserAvatar
await AttachmentModel.updateOne(
{ _id: user.avatar },
{
$unset: {
context: '',
ownerType: '',
owner: '',
},
},
);
logger.log(`User ${user._id} avatar attributes successfully reverted`);
} catch (error) {
logger.error(
`Failed to revert avatar attributes for user ${user._id}: ${error.message}`,
);
}
}
};
/**
* Updates subscriber documents with their corresponding avatar attachments,
* populate new attributes (context, owner, ownerType) and moves avatar files to a new directory.
* *
* @returns Resolves when the migration process is complete. * @returns Resolves when the migration process is complete.
*/ */
@ -539,6 +614,7 @@ const migrateAttachmentMessages = async ({
module.exports = { module.exports = {
async up(services: MigrationServices) { async up(services: MigrationServices) {
await populateUserAvatar(services);
await populateSubscriberAvatar(services); await populateSubscriberAvatar(services);
await updateOldAvatarsPath(services); await updateOldAvatarsPath(services);
await migrateAttachmentBlocks(MigrationAction.UP, services); await migrateAttachmentBlocks(MigrationAction.UP, services);
@ -549,6 +625,7 @@ module.exports = {
return true; return true;
}, },
async down(services: MigrationServices) { async down(services: MigrationServices) {
await unpopulateUserAvatar(services);
await unpopulateSubscriberAvatar(services); await unpopulateSubscriberAvatar(services);
await restoreOldAvatarsPath(services); await restoreOldAvatarsPath(services);
await migrateAttachmentBlocks(MigrationAction.DOWN, services); await migrateAttachmentBlocks(MigrationAction.DOWN, services);