From 8ddeaa2b32a3cfec9bad9f42ad71876f2b3115bd Mon Sep 17 00:00:00 2001 From: Benjamin Piouffle Date: Thu, 24 Oct 2024 12:43:04 +0200 Subject: [PATCH] fix(Webhooks): account association --- .../20241024103852-remove-invalid-webhooks.js | 40 +++++++++++++++++++ .../graphql/v2/mutation/WebhookMutations.js | 2 +- server/graphql/v2/object/Webhook.js | 4 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 migrations/20241024103852-remove-invalid-webhooks.js diff --git a/migrations/20241024103852-remove-invalid-webhooks.js b/migrations/20241024103852-remove-invalid-webhooks.js new file mode 100644 index 00000000000..b2761e79203 --- /dev/null +++ b/migrations/20241024103852-remove-invalid-webhooks.js @@ -0,0 +1,40 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface) { + const webhooks = await queryInterface.sequelize.query( + ` + DELETE FROM "Notifications" + WHERE channel = 'webhook' + AND "CollectiveId" IS NULL + RETURNING * + `, + { + type: queryInterface.sequelize.QueryTypes.SELECT, + }, + ); + + await queryInterface.sequelize.query( + ` + INSERT INTO "MigrationLogs" + ("createdAt", "type", "description", "CreatedByUserId", "data") + VALUES ( + NOW(), + 'MIGRATION', + 'migrations/20241024103852-remove-invalid-webhooks', + NULL, + :data + ) + `, + { + replacements: { data: JSON.stringify(webhooks) }, + type: queryInterface.sequelize.QueryTypes.INSERT, + }, + ); + }, + + async down() { + console.log(`No restore for this migration, look at the migration log for the deleted webhooks`); + }, +}; diff --git a/server/graphql/v2/mutation/WebhookMutations.js b/server/graphql/v2/mutation/WebhookMutations.js index e16aa1602e1..18800d3f5e8 100644 --- a/server/graphql/v2/mutation/WebhookMutations.js +++ b/server/graphql/v2/mutation/WebhookMutations.js @@ -38,7 +38,7 @@ const createWebhook = { type: args.webhook.activityType, webhookUrl: args.webhook.webhookUrl, UserId: req.remoteUser.id, - CollectiveId: account.CollectiveId, + CollectiveId: account.id, }; return models.Notification.create(createParams); diff --git a/server/graphql/v2/object/Webhook.js b/server/graphql/v2/object/Webhook.js index b9ce02d7755..bbeac34526f 100644 --- a/server/graphql/v2/object/Webhook.js +++ b/server/graphql/v2/object/Webhook.js @@ -36,7 +36,9 @@ export const GraphQLWebhook = new GraphQLObjectType({ account: { type: new GraphQLNonNull(GraphQLAccount), resolve(notification, args, req) { - return req.loaders.Collective.byId.load(notification.CollectiveId); + if (notification.CollectiveId) { + return req.loaders.Collective.byId.load(notification.CollectiveId); + } }, }, }),