Browse Source

FIX | fixed chats if order is taken

merge-requests/207/head
Vitalik 2 years ago
parent
commit
72a8226b6d
  1. 24
      src/domain/chats/services/chats-fixed.service.ts

24
src/domain/chats/services/chats-fixed.service.ts

@ -18,11 +18,31 @@ export class ChatsFixedService implements Chats.IChatsFixedService { @@ -18,11 +18,31 @@ export class ChatsFixedService implements Chats.IChatsFixedService {
if (fixedCount >= this.MAX_FIXED_CHATS) throw new BadRequestException('Max fixed chats')
const orderTaken = await this.chatsFixedRepository.findOne({
userId,
order,
})
let itemsToRecalcOrder: Chats.IChatFixed[] = []
if (orderTaken)
itemsToRecalcOrder = await this.chatsFixedRepository
.createQueryBuilder('it')
.where('it.userId = :userId', { userId })
.andWhere('it.order >= :order', { order })
.getMany()
await this.chatsFixedRepository.insert({
userId,
chatId,
order: _.defaultTo(Number(order), 0),
order: _.defaultTo(Number(order), 1),
})
if (orderTaken) {
for await (const [index, item] of itemsToRecalcOrder.entries()) {
await this.chatsFixedRepository.update(item.id, { order: order + index + 1 })
}
}
}
public async unfix(userId: number, chatId: number) {
@ -31,7 +51,7 @@ export class ChatsFixedService implements Chats.IChatsFixedService { @@ -31,7 +51,7 @@ export class ChatsFixedService implements Chats.IChatsFixedService {
const fixedChats = await this.getFixed(userId)
let order = 0
let order = 1
for await (const item of fixedChats) {
await this.chatsFixedRepository.update(item.id, { order })

Loading…
Cancel
Save