getAnnouncements の順番を入れ替え

This commit is contained in:
riku6460 2023-10-11 04:02:49 +09:00
parent 1e56bdc891
commit 019223e9ec
No known key found for this signature in database
GPG key ID: 27414FA27DB94CF6

View file

@ -42,83 +42,19 @@ export class AnnouncementService {
}); });
} }
@bindThis
public async getAnnouncements(
me: MiUser | null,
limit: number,
offset: number,
isActive?: boolean,
): Promise<Packed<'Announcement'>[]> {
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<MiAnnouncement & { isRead?: boolean | null }>(),
me,
);
}
@bindThis @bindThis
public async getUnreadAnnouncements(user: MiUser): Promise<Packed<'Announcement'>[]> { public async getUnreadAnnouncements(user: MiUser): Promise<Packed<'Announcement'>[]> {
const query = this.announcementsRepository.createQueryBuilder('announcement'); const q = this.announcementsRepository.createQueryBuilder('announcement');
query.leftJoin( q.leftJoin(
MiAnnouncementRead, MiAnnouncementRead,
'read', 'read',
'read."announcementId" = announcement.id AND read."userId" = :userId', 'read."announcementId" = announcement.id AND read."userId" = :userId',
{ userId: user.id }, { userId: user.id },
); );
query.andWhere('read.id IS NULL'); q.andWhere('read.id IS NULL');
query.andWhere('announcement."isActive" = true');
query q
.where('announcement.isActive = true')
.andWhere(new Brackets(qb => { .andWhere(new Brackets(qb => {
qb.orWhere('announcement.userId = :userId', { userId: user.id }); qb.orWhere('announcement.userId = :userId', { userId: user.id });
qb.orWhere('announcement.userId IS NULL'); qb.orWhere('announcement.userId IS NULL');
@ -128,13 +64,13 @@ export class AnnouncementService {
qb.orWhere('announcement.createdAt > :createdAt', { createdAt: user.createdAt }); qb.orWhere('announcement.createdAt > :createdAt', { createdAt: user.createdAt });
})); }));
query.orderBy({ q.orderBy({
'announcement."displayOrder"': 'DESC', 'announcement."displayOrder"': 'DESC',
'announcement."createdAt"': 'DESC', 'announcement."createdAt"': 'DESC',
}); });
return this.packMany( return this.packMany(
await query.getMany(), await q.getMany(),
user, user,
); );
} }
@ -314,6 +250,70 @@ export class AnnouncementService {
} }
} }
@bindThis
public async getAnnouncements(
me: MiUser | null,
limit: number,
offset: number,
isActive?: boolean,
): Promise<Packed<'Announcement'>[]> {
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<MiAnnouncement & { isRead?: boolean | null }>(),
me,
);
}
@bindThis @bindThis
public async countUnreadAnnouncements(me: MiUser): Promise<number> { public async countUnreadAnnouncements(me: MiUser): Promise<number> {
const query = this.announcementsRepository.createQueryBuilder('announcement'); const query = this.announcementsRepository.createQueryBuilder('announcement');