diff --git a/server/models/base/Model.ts b/server/models/base/Model.ts index 52ef3bec04..ac0cb7078b 100644 --- a/server/models/base/Model.ts +++ b/server/models/base/Model.ts @@ -289,6 +289,7 @@ class Model< }, callback: (results: Array, query: FindOptions) => Promise ): Promise { + let total = 0; const mappedQuery = { ...query, offset: query.offset ?? 0, @@ -300,6 +301,7 @@ class Model< do { // @ts-expect-error this T results = await this.findAll(mappedQuery); + total += results.length; await callback(results, mappedQuery); mappedQuery.offset += mappedQuery.limit; } while ( @@ -307,7 +309,7 @@ class Model< (mappedQuery.totalLimit ?? Infinity) > mappedQuery.offset ); - return mappedQuery.offset; + return total; } /** diff --git a/server/routes/api/notifications/notifications.ts b/server/routes/api/notifications/notifications.ts index f3c95bb437..9a3766d090 100644 --- a/server/routes/api/notifications/notifications.ts +++ b/server/routes/api/notifications/notifications.ts @@ -1,4 +1,5 @@ import Router from "koa-router"; +import isEmpty from "lodash/isEmpty"; import isNil from "lodash/isNil"; import isNull from "lodash/isNull"; import isUndefined from "lodash/isUndefined"; @@ -180,7 +181,10 @@ router.post( const { transaction } = ctx.state; const notification = await Notification.findByPk(id, { - lock: transaction.LOCK.UPDATE, + lock: { + level: transaction.LOCK.UPDATE, + of: Notification, + }, rejectOnEmpty: true, transaction, }); @@ -229,14 +233,19 @@ router.post( }; } - const total = await Notification.findAllInBatches( - { where }, - async (results) => { - await Promise.all( - results.map((notification) => notification.updateWithCtx(ctx, values)) - ); - } - ); + let total = 0; + if (!isEmpty(values)) { + total = await Notification.findAllInBatches( + { where }, + async (results) => { + await Promise.all( + results.map((notification) => + notification.updateWithCtx(ctx, values) + ) + ); + } + ); + } ctx.body = { success: true,