Skip to content

Commit

Permalink
Merge pull request #4852 from bcgov/feat/4846
Browse files Browse the repository at this point in the history
chore(4771): add AG ministry Webhook URL duplication script
  • Loading branch information
junminahn authored Feb 4, 2025
2 parents ace33dc + 1dc8ed6 commit 84d859d
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export const up = async (db, client) => {
const session = client.startSession();

await session.withTransaction(async () => {
const PrivateCloudProductWebhook = db.collection('PrivateCloudProductWebhook');
const PrivateCloudProject = db.collection('PrivateCloudProject');

const baseWebhook = await PrivateCloudProductWebhook.findOne({ licencePlate: { $eq: 'e5ced5' } });
if (!baseWebhook) return;

const targetProducts = await PrivateCloudProject.find({
ministry: { $in: ['AG', 'PSSG', 'EMBC'] },
status: { $eq: 'ACTIVE' },
})
.project({ licencePlate: 1 })
.toArray();

const webhookDocsToCopy = targetProducts.map(({ licencePlate }) => {
return {
licencePlate,
url: baseWebhook.url,
secret: baseWebhook.secret ?? '',
username: baseWebhook.username ?? '',
password: baseWebhook.password ?? '',
};
});

if (webhookDocsToCopy.length === 0) return;

const targetLicensePlates = targetProducts.map(({ licencePlate }) => licencePlate);
await PrivateCloudProductWebhook.deleteMany({ licencePlate: { $in: targetLicensePlates } });

const result = await PrivateCloudProductWebhook.insertMany(webhookDocsToCopy, {});
console.log('renamag_ministry_webhook_duplicate:', result);
});

session.endSession();
};

export const down = async (db, client) => {};

0 comments on commit 84d859d

Please sign in to comment.