From 019223e9ec9af30b06eccf17f829f3d94e8dfd31 Mon Sep 17 00:00:00 2001 From: riku6460 <17585784+riku6460@users.noreply.github.com> Date: Wed, 11 Oct 2023 04:02:49 +0900 Subject: [PATCH] =?UTF-8?q?getAnnouncements=20=E3=81=AE=E9=A0=86=E7=95=AA?= =?UTF-8?q?=E3=82=92=E5=85=A5=E3=82=8C=E6=9B=BF=E3=81=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/core/AnnouncementService.ts | 142 +++++++++--------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/packages/backend/src/core/AnnouncementService.ts b/packages/backend/src/core/AnnouncementService.ts index 6927a89655..9c6cda6c0d 100644 --- a/packages/backend/src/core/AnnouncementService.ts +++ b/packages/backend/src/core/AnnouncementService.ts @@ -42,83 +42,19 @@ export class AnnouncementService { }); } - @bindThis - public async getAnnouncements( - me: MiUser | null, - limit: number, - offset: number, - isActive?: boolean, - ): Promise[]> { - const query = this.announcementsRepository.createQueryBuilder('announcement'); - if (me) { - query.leftJoin( - MiAnnouncementRead, - 'read', - 'read."announcementId" = announcement.id AND read."userId" = :userId', - { userId: me.id }, - ); - query.select([ - 'announcement.*', - 'read.id IS NOT NULL as "isRead"', - ]); - query - .andWhere( - new Brackets((qb) => { - qb.orWhere('announcement."userId" = :userId', { userId: me.id }); - qb.orWhere('announcement."userId" IS NULL'); - }), - ) - .andWhere( - new Brackets((qb) => { - qb.orWhere('announcement."forExistingUsers" = false'); - qb.orWhere('announcement."createdAt" > :createdAt', { - createdAt: me.createdAt, - }); - }), - ); - } else { - query.select([ - 'announcement.*', - 'NULL as "isRead"', - ]); - query.andWhere('announcement."userId" IS NULL'); - query.andWhere('announcement."forExistingUsers" = false'); - } - - if (isActive !== undefined) { - query.andWhere('announcement."isActive" = :isActive', { - isActive: isActive, - }); - } - - query.orderBy({ - '"isRead"': 'ASC', - 'announcement."displayOrder"': 'DESC', - 'announcement."createdAt"': 'DESC', - }); - - return this.packMany( - await query - .limit(limit) - .offset(offset) - .getRawMany(), - me, - ); - } - @bindThis public async getUnreadAnnouncements(user: MiUser): Promise[]> { - const query = this.announcementsRepository.createQueryBuilder('announcement'); - query.leftJoin( + const q = this.announcementsRepository.createQueryBuilder('announcement'); + q.leftJoin( MiAnnouncementRead, 'read', 'read."announcementId" = announcement.id AND read."userId" = :userId', { userId: user.id }, ); - query.andWhere('read.id IS NULL'); - query.andWhere('announcement."isActive" = true'); + q.andWhere('read.id IS NULL'); - query + q + .where('announcement.isActive = true') .andWhere(new Brackets(qb => { qb.orWhere('announcement.userId = :userId', { userId: user.id }); qb.orWhere('announcement.userId IS NULL'); @@ -128,13 +64,13 @@ export class AnnouncementService { qb.orWhere('announcement.createdAt > :createdAt', { createdAt: user.createdAt }); })); - query.orderBy({ + q.orderBy({ 'announcement."displayOrder"': 'DESC', 'announcement."createdAt"': 'DESC', }); return this.packMany( - await query.getMany(), + await q.getMany(), user, ); } @@ -314,6 +250,70 @@ export class AnnouncementService { } } + @bindThis + public async getAnnouncements( + me: MiUser | null, + limit: number, + offset: number, + isActive?: boolean, + ): Promise[]> { + const query = this.announcementsRepository.createQueryBuilder('announcement'); + if (me) { + query.leftJoin( + MiAnnouncementRead, + 'read', + 'read."announcementId" = announcement.id AND read."userId" = :userId', + { userId: me.id }, + ); + query.select([ + 'announcement.*', + 'read.id IS NOT NULL as "isRead"', + ]); + query + .andWhere( + new Brackets((qb) => { + qb.orWhere('announcement."userId" = :userId', { userId: me.id }); + qb.orWhere('announcement."userId" IS NULL'); + }), + ) + .andWhere( + new Brackets((qb) => { + qb.orWhere('announcement."forExistingUsers" = false'); + qb.orWhere('announcement."createdAt" > :createdAt', { + createdAt: me.createdAt, + }); + }), + ); + } else { + query.select([ + 'announcement.*', + 'NULL as "isRead"', + ]); + query.andWhere('announcement."userId" IS NULL'); + query.andWhere('announcement."forExistingUsers" = false'); + } + + if (isActive !== undefined) { + query.andWhere('announcement."isActive" = :isActive', { + isActive: isActive, + }); + } + + query.orderBy({ + '"isRead"': 'ASC', + 'announcement."displayOrder"': 'DESC', + 'announcement."createdAt"': 'DESC', + }); + + return this.packMany( + await query + .limit(limit) + .offset(offset) + .getRawMany(), + me, + ); + } + @bindThis public async countUnreadAnnouncements(me: MiUser): Promise { const query = this.announcementsRepository.createQueryBuilder('announcement');