import { Project } from 'nexus-plugin-prisma/client' import * as SendBirdController from '../../controllers/sendbird' import { prismaClient as prisma } from '../../prismaClient' const PROJECT_ID_TO_MOVE = 'cjx3ansgl027u0754xax9gekz' const NEW_MANAGING_COMPANY_ID_FOR_PROJECT = 'ck19m0gw6cjvy07133bcr31u8' const OLD_MANAGING_COMPANY_ID_FOR_PROJECT = 'cjvi6mt2o00zx07984eau41nm' const DELETE_MEMOS = false const DELETE_EVENTS = false const DELETE_TASKS = false const DELETE_FILES_AND_FOLDERS = false const DELETE_CONTACTS = false const DELETE_AMENITIES = false const CONTINUE_CONVERSATIONS = true prisma.project.findOne({ where: { id: PROJECT_ID_TO_MOVE } }).then(async (project: Project) => { try { console.log('Moving project', project.name) if (DELETE_MEMOS) { console.log('Deleting post for this project') await prisma.post.deleteMany({ where: { project: { id: PROJECT_ID_TO_MOVE } } }) } if (DELETE_EVENTS) { } if (DELETE_TASKS) { } if (DELETE_FILES_AND_FOLDERS) { } if (DELETE_CONTACTS) { } if (DELETE_AMENITIES) { } console.log('Putting project in new company') await prisma.project.update({ where: { id: project.id }, data: { managingCompany: { connect: { id: NEW_MANAGING_COMPANY_ID_FOR_PROJECT, }, }, }, }) console.log('Fixing roles') const rolesOfNewManagingCompany = await prisma.managingCompanyRole.findMany({ where: { managingCompany: { id: NEW_MANAGING_COMPANY_ID_FOR_PROJECT, }, }, }) // Roles of new managing company can now see the project await Promise.all( rolesOfNewManagingCompany.map((role) => prisma.managingCompanyRole.update({ where: { id: role.id }, data: { projects: { connect: [ { id: project.id, }, ], }, }, }) ) ) // Conversations // Every conversations should be kept // In the resident app, user shouldn't be able to send message to the old conversation if (CONTINUE_CONVERSATIONS) { const usersOfNewManagingCompany = await prisma.user.findMany({ where: { managingCompany: { id: NEW_MANAGING_COMPANY_ID_FOR_PROJECT, }, }, }) const usersOfOldManagingCompany = await prisma.user.findMany({ where: { managingCompany: { id: OLD_MANAGING_COMPANY_ID_FOR_PROJECT, }, }, }) const usersOfNewManagingCompanyIds = usersOfNewManagingCompany.map(({ id }) => id) const usersOfOldManagingCompanyIds = usersOfOldManagingCompany.map(({ id }) => id) // Remove old managing company as member and put the new one const channels = await SendBirdController.getChannelsForUserId({ userId: OLD_MANAGING_COMPANY_ID_FOR_PROJECT, all: true, show_member: true, }) const channelsForProject = channels.filter(({ name }) => name.startsWith(project.id)) console.log('Processing channels:', channelsForProject.length) for await (const channel of channelsForProject) { const oldManagingCompanyMember = channel.members.find( ({ user_id: userId }) => userId === OLD_MANAGING_COMPANY_ID_FOR_PROJECT ) if (oldManagingCompanyMember) { console.log('Processing channel:', channel.name) // public await SendBirdController.removeUsersFromChannel({ userIds: [oldManagingCompanyMember.user_id], channelUrl: channel.channel_url, }) await SendBirdController.addUsersToChannel({ userIds: [NEW_MANAGING_COMPANY_ID_FOR_PROJECT], channelUrl: channel.channel_url, }) // private const privateChannel = await SendBirdController.getChannelByName({ name: `${channel.name}-private`, }) if (privateChannel) { await SendBirdController.addUsersToChannel({ userIds: usersOfNewManagingCompanyIds, channelUrl: privateChannel.channel_url, }) await SendBirdController.removeUsersFromChannel({ userIds: usersOfOldManagingCompanyIds, channelUrl: privateChannel.channel_url, }) } } } } console.log('done') } catch (error) { console.error(error) } })