import '../../config' import { Project } from 'nexus-plugin-prisma/client' import * as SendBirdController from '../../controllers/sendbird' import { prismaClient as prisma } from '../../prismaClient' // For each Channels // If the property's building is no longer linked to a project // Check if there's a property with the same address number in the project // Change the name of the channel for this property id instead prisma.project .findOne({ where: { id: 'cjx28nooq06bm0756m1qmwwv2' } }) .then(async (project: Project) => { const channels = await SendBirdController.getChannelsForUserId({ userId: 'cju0n3mci00880852wdlqydou', name_startswith: project.id, }) for await (const channel of channels) { const [, propertyId] = channel.name.split('-') // The current wrong (maybe) property linked to channel const propertyForChannel = await prisma.property.findOne({ where: { id: propertyId }, include: { address: true, building: { include: { project: true } } }, }) if (!propertyForChannel.building.project) { const otherPropertyWithSameUnitNumber = await prisma.property.findMany({ where: { id: { not: propertyId, }, building: { project: { id: project.id } }, address: { apartmentNumber: propertyForChannel.address.apartmentNumber }, }, take: 1, include: { address: true, }, }) if (otherPropertyWithSameUnitNumber.length) { const oldName = channel.name const newName = `${project.id}-${otherPropertyWithSameUnitNumber[0].id}` await SendBirdController.updatechannel({ channelUrl: channel.channel_url, data: { name: newName }, }) // Also change the name of the private channel const privateChannel = await SendBirdController.getManagerChannel({ propertyId: propertyForChannel.id, projectId: project.id, isPrivateChannel: true, }) if (privateChannel) { const newPrivateName = `${project.id}-${otherPropertyWithSameUnitNumber[0].id}-private` await SendBirdController.updatechannel({ channelUrl: channel.channel_url, data: { name: newPrivateName }, }) } } } } })