From 4fd19487d62322740ae0f8fb0bf5702d0b58f872 Mon Sep 17 00:00:00 2001 From: Ignazio Bovo Date: Fri, 24 Nov 2023 15:41:08 +0100 Subject: [PATCH] feat: :zap: batch channel verification --- .../resolvers/ChannelsResolver/index.ts | 94 ++++++++++--------- .../resolvers/ChannelsResolver/types.ts | 4 +- 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/server-extension/resolvers/ChannelsResolver/index.ts b/src/server-extension/resolvers/ChannelsResolver/index.ts index ca4adac52..1d977061c 100644 --- a/src/server-extension/resolvers/ChannelsResolver/index.ts +++ b/src/server-extension/resolvers/ChannelsResolver/index.ts @@ -1,6 +1,6 @@ import 'reflect-metadata' import { Args, Query, Mutation, Resolver, Info, Ctx, UseMiddleware } from 'type-graphql' -import { EntityManager, IsNull } from 'typeorm' +import { ArrayContains, EntityManager, IsNull } from 'typeorm' import { ChannelNftCollector, ChannelNftCollectorsArgs, @@ -394,9 +394,9 @@ export class ChannelsResolver { @Mutation(() => VerifyChannelResult) @UseMiddleware(OperatorOnly()) - async verifyChannel(@Args() { channelId }: VerifyChannelArgs): Promise { + async verifyChannel(@Args() { channelIds }: VerifyChannelArgs): Promise { const em = await this.em() - return await verifyChannelService(em, channelId) + return await verifyChannelService(em, channelIds) } @Mutation(() => ExcludeChannelResult) @@ -468,51 +468,55 @@ export const excludeChannelService = async ( }) } -export const verifyChannelService = async (em: EntityManager, channelId: string) => { +export const verifyChannelService = async (em: EntityManager, channelIds: string[]) => { return withHiddenEntities(em, async () => { - const channel = await em.findOne(Channel, { - where: { id: channelId }, - }) - - if (!channel) { - throw new Error(`Channel by id ${channelId} not found!`) - } - // If channel already verified - return its data - if (channel.yppStatus.isTypeOf === 'YppVerified') { - const existingVerification = await em.getRepository(ChannelVerification).findOneOrFail({ - where: { channelId: channel.id }, - }) - return { - id: existingVerification.id, - channelId: channel.id, - createdAt: existingVerification.timestamp, - } - } - // othewise create new verification - const newVerification = new ChannelVerification({ - id: uniqueId(), - channelId: channel.id, - timestamp: new Date(), + const channels = await em.getRepository(Channel).find({ + where: { id: ArrayContains(channelIds) }, }) - channel.yppStatus = new YppVerified({ verification: newVerification.id }) - await em.save([newVerification, channel]) - // in case account exist deposit notification - const channelOwnerMemberId = channel.ownerMemberId - if (channelOwnerMemberId) { - const account = await em.findOne(Account, { where: { membershipId: channelOwnerMemberId } }) - await addNotification( - em, - account, - new ChannelRecipient({ channel: channel.id }), - new ChannelVerified({}) - ) - } + const results = channels + .filter((channel) => channel) + .map(async (channel) => { + // If channel already verified - return its data + if (channel.yppStatus.isTypeOf === 'YppVerified') { + const existingVerification = await em.getRepository(ChannelVerification).findOneOrFail({ + where: { channelId: channel.id }, + }) + return { + id: existingVerification.id, + channelId: channel.id, + createdAt: existingVerification.timestamp, + } + } + // othewise create new verification regardless whether the channel was previously verified + const newVerification = new ChannelVerification({ + id: uniqueId(), + channelId: channel.id, + timestamp: new Date(), + }) + channel.yppStatus = new YppVerified({ verification: newVerification.id }) + await em.save([newVerification, channel]) + + // in case account exist deposit notification + const channelOwnerMemberId = channel.ownerMemberId + if (channelOwnerMemberId) { + const account = await em.findOne(Account, { + where: { membershipId: channelOwnerMemberId }, + }) + await addNotification( + em, + account, + new ChannelRecipient({ channel: channel.id }), + new ChannelVerified({}) + ) + } - return { - id: newVerification.id, - channelId: channel.id, - createdAt: newVerification.timestamp, - } + return { + id: newVerification.id, + channelId: channel.id, + createdAt: newVerification.timestamp, + } + }) + return await Promise.all(results) }) } diff --git a/src/server-extension/resolvers/ChannelsResolver/types.ts b/src/server-extension/resolvers/ChannelsResolver/types.ts index 35964bef8..82594cad2 100644 --- a/src/server-extension/resolvers/ChannelsResolver/types.ts +++ b/src/server-extension/resolvers/ChannelsResolver/types.ts @@ -186,8 +186,8 @@ export class SuspendChannelArgs { @ArgsType() export class VerifyChannelArgs { - @Field(() => String, { nullable: false }) - channelId!: string + @Field(() => [String], { nullable: false }) + channelIds!: string[] } @ObjectType()