diff --git a/services/api/src/resources/notification/sql.js b/services/api/src/resources/notification/sql.js index e9348e94e9..8a8a7b14cd 100644 --- a/services/api/src/resources/notification/sql.js +++ b/services/api/src/resources/notification/sql.js @@ -32,23 +32,21 @@ const Sql /* : SqlObj */ = { .select('nt.*', 'pn.*', knex.raw('? as type', [type])) .toString(); }, - deleteProjectNotification: ( - input /* : Object */, - ) => { - const { project, notificationType, notificationName } = input; - - const deleteQuery = knex('project_notification AS pn') - .joinRaw( - `LEFT JOIN notification_${notificationType} AS nt ON pn.nid = nt.id AND pn.type = ?`, - [notificationType], - ) - .leftJoin('project AS p', 'pn.pid', '=', 'p.id'); + deleteProjectNotification: (input /* : Object */) => { + const deleteQuery = knex.raw( + `DELETE pn + FROM project_notification as pn + LEFT JOIN :notificationTable: AS nt ON pn.nid = nt.id AND pn.type = :notificationType + LEFT JOIN project as p on pn.pid = p.id + WHERE p.name = :project + AND nt.name = :notificationName`, + { + ...input, + notificationTable: `notification_${input.notificationType}`, + }, + ); - return deleteQuery - .where('p.name', project) - .andWhere('nt.name', notificationName) - .del() - .toString(); + return deleteQuery.toString(); }, selectProjectById: (input /* : Object */) => knex('project') @@ -83,9 +81,7 @@ const Sql /* : SqlObj */ = { .update(patch) .toString(); }, - selectNotificationsByTypeByProjectId: ( - input /* : Object */, - ) => { + selectNotificationsByTypeByProjectId: (input /* : Object */) => { const { type, pid } = input; const selectQuery = knex('project_notification AS pn').joinRaw( `JOIN notification_${type} AS nt ON pn.nid = nt.id AND pn.type = ?`,