diff --git a/src/services/impl/notifications/find-all-notifications.ts b/src/services/impl/notifications/find-all-notifications.ts
index bf033c4f3dfe4124258bbe9f1ef0f458acf5a73d..8d56380d20d36faf1e2e6d58c55bd47143eb7d98 100644
--- a/src/services/impl/notifications/find-all-notifications.ts
+++ b/src/services/impl/notifications/find-all-notifications.ts
@@ -1,65 +1,77 @@
-import { Command } from "../command";
-import { Brackets, EntityManager } from "typeorm";
-import { Notification } from "../../../models/notification";
-import { Channel } from "../../../models/channel";
-import { User } from "../../../models/user";
-import { ForbiddenError, NotFoundError } from "routing-controllers";
-import { AuthorizationBag } from "../../../models/authorization-bag";
+import { Command } from '../command';
+import { Brackets, EntityManager } from 'typeorm';
+import { Notification } from '../../../models/notification';
+import { Channel } from '../../../models/channel';
+import { User } from '../../../models/user';
+import { ForbiddenError, NotFoundError } from 'routing-controllers';
+import { AuthorizationBag } from '../../../models/authorization-bag';
export class FindAllNotifications implements Command {
- constructor(private channelId: string, private query: any, private authorizationBag: AuthorizationBag) { }
+ constructor(
+ private channelId: string,
+ private query: any,
+ private authorizationBag: AuthorizationBag,
+ ) {}
async execute(transactionManager: EntityManager) {
const channel = await transactionManager.findOne(Channel, {
// Specify needed joinColumns targets here, or use eager=true in model column def.
- relations: ["members", "groups", "owner", "adminGroup"],
+ relations: ['members', 'groups', 'owner', 'adminGroup'],
where: {
id: this.channelId,
},
});
- if (!channel)
- throw new NotFoundError("Channel does not exist");
+ if (!channel) throw new NotFoundError('Channel does not exist');
if (!(await channel.hasAccess(this.authorizationBag)))
- throw new ForbiddenError("Access to Channel not Authorized!");
+ throw new ForbiddenError('Access to Channel not Authorized!');
let isAdmin = await channel.isAdmin(this.authorizationBag);
let userCanSend = await channel.canSendByForm(this.authorizationBag);
- const notificationQB = await transactionManager.getRepository(Notification)
- .createQueryBuilder("notification")
- .where("notification.target = :chanId", { chanId: this.channelId })
- .orderBy("notification.sentAt", "DESC");
+ const notificationQB = await transactionManager
+ .getRepository(Notification)
+ .createQueryBuilder('notification')
+ .where('notification.target = :chanId', { chanId: this.channelId })
+ .orderBy('notification.sentAt', 'DESC', 'NULLS FIRST')
+ .addOrderBy('notification.sendAt', 'ASC');
if (this.query.searchText) {
- notificationQB.andWhere(new Brackets(notificationQB => {
- notificationQB.where(
- "notification.summary Like :searchText or notification.body Like :searchText",
- { searchText: `%${this.query.searchText}%` }
- )
- }));
+ notificationQB.andWhere(
+ new Brackets(notificationQB => {
+ notificationQB.where(
+ 'notification.summary Like :searchText or notification.body Like :searchText',
+ { searchText: `%${this.query.searchText}%` },
+ );
+ }),
+ );
}
if (!isAdmin) {
if (userCanSend) {
- notificationQB.andWhere(new Brackets(notificationQB => {
- notificationQB.where("notification.sendAt is null")
- .orWhere(new Brackets(notificationQB => {
- notificationQB.where("notification.sendAt is not null and notification.sentAt is null and notification.sender = :s",
- {s: this.authorizationBag.email})
- }))
- }));
- }
- else {
- notificationQB.andWhere("notification.sendAt is null");
+ notificationQB.andWhere(
+ new Brackets(notificationQB => {
+ notificationQB.where('notification.sendAt is null').orWhere(
+ new Brackets(notificationQB => {
+ notificationQB.where(
+ 'notification.sendAt is not null and notification.sentAt is null and notification.sender = :s',
+ { s: this.authorizationBag.email },
+ );
+ }),
+ );
+ }),
+ );
+ } else {
+ notificationQB.andWhere('notification.sendAt is null');
}
}
notificationQB.skip(this.query.skip);
notificationQB.take(this.query.take);
- let [notifications, numberOfNotifications] = await notificationQB.getManyAndCount();
+ let [notifications, numberOfNotifications] =
+ await notificationQB.getManyAndCount();
return {
items: notifications,