chore: addition of new GQL subscription for UserCollection duplication (#4358)
Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com>
This commit is contained in:
@@ -23,6 +23,7 @@ import { TeamInvitation } from 'src/team-invitation/team-invitation.model';
|
|||||||
import { InvitedUser } from '../admin/invited-user.model';
|
import { InvitedUser } from '../admin/invited-user.model';
|
||||||
import {
|
import {
|
||||||
UserCollection,
|
UserCollection,
|
||||||
|
UserCollectionDuplicatedData,
|
||||||
UserCollectionRemovedData,
|
UserCollectionRemovedData,
|
||||||
UserCollectionReorderData,
|
UserCollectionReorderData,
|
||||||
} from 'src/user-collection/user-collections.model';
|
} from 'src/user-collection/user-collections.model';
|
||||||
@@ -48,6 +49,7 @@ export type TopicDef = {
|
|||||||
[
|
[
|
||||||
topic: `user_coll/${string}/${'created' | 'updated' | 'moved'}`
|
topic: `user_coll/${string}/${'created' | 'updated' | 'moved'}`
|
||||||
]: UserCollection;
|
]: UserCollection;
|
||||||
|
[topic: `user_coll/${string}/${'duplicated'}`]: UserCollectionDuplicatedData;
|
||||||
[topic: `user_coll/${string}/${'deleted'}`]: UserCollectionRemovedData;
|
[topic: `user_coll/${string}/${'deleted'}`]: UserCollectionRemovedData;
|
||||||
[topic: `user_coll/${string}/${'order_updated'}`]: UserCollectionReorderData;
|
[topic: `user_coll/${string}/${'order_updated'}`]: UserCollectionReorderData;
|
||||||
[topic: `team/${string}/member_removed`]: string;
|
[topic: `team/${string}/member_removed`]: string;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import { AuthUser } from 'src/types/AuthUser';
|
|||||||
import { UserCollectionService } from './user-collection.service';
|
import { UserCollectionService } from './user-collection.service';
|
||||||
import {
|
import {
|
||||||
UserCollection,
|
UserCollection,
|
||||||
|
UserCollectionDuplicatedData,
|
||||||
UserCollectionExportJSONData,
|
UserCollectionExportJSONData,
|
||||||
UserCollectionRemovedData,
|
UserCollectionRemovedData,
|
||||||
UserCollectionReorderData,
|
UserCollectionReorderData,
|
||||||
@@ -470,4 +471,14 @@ export class UserCollectionResolver {
|
|||||||
userCollectionOrderUpdated(@GqlUser() user: AuthUser) {
|
userCollectionOrderUpdated(@GqlUser() user: AuthUser) {
|
||||||
return this.pubSub.asyncIterator(`user_coll/${user.uid}/order_updated`);
|
return this.pubSub.asyncIterator(`user_coll/${user.uid}/order_updated`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscription(() => UserCollectionDuplicatedData, {
|
||||||
|
description: 'Listen to when a User Collection has been duplicated',
|
||||||
|
resolve: (value) => value,
|
||||||
|
})
|
||||||
|
@SkipThrottle()
|
||||||
|
@UseGuards(GqlAuthGuard)
|
||||||
|
userCollectionDuplicated(@GqlUser() user: AuthUser) {
|
||||||
|
return this.pubSub.asyncIterator(`user_coll/${user.uid}/duplicated`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import { Prisma, UserCollection, ReqType as DBReqType } from '@prisma/client';
|
|||||||
import {
|
import {
|
||||||
UserCollection as UserCollectionModel,
|
UserCollection as UserCollectionModel,
|
||||||
UserCollectionExportJSONData,
|
UserCollectionExportJSONData,
|
||||||
|
UserCollectionDuplicatedData,
|
||||||
} from './user-collections.model';
|
} from './user-collections.model';
|
||||||
import { ReqType } from 'src/types/RequestTypes';
|
import { ReqType } from 'src/types/RequestTypes';
|
||||||
import {
|
import {
|
||||||
@@ -1035,6 +1036,7 @@ export class UserCollectionService {
|
|||||||
userID: string,
|
userID: string,
|
||||||
destCollectionID: string | null,
|
destCollectionID: string | null,
|
||||||
reqType: DBReqType,
|
reqType: DBReqType,
|
||||||
|
isCollectionDuplication = false,
|
||||||
) {
|
) {
|
||||||
// Check to see if jsonString is valid
|
// Check to see if jsonString is valid
|
||||||
const collectionsList = stringToJson<CollectionFolder[]>(jsonString);
|
const collectionsList = stringToJson<CollectionFolder[]>(jsonString);
|
||||||
@@ -1087,9 +1089,24 @@ export class UserCollectionService {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
userCollections.forEach((collection) =>
|
if (isCollectionDuplication) {
|
||||||
this.pubsub.publish(`user_coll/${userID}/created`, this.cast(collection)),
|
const collectionData = await this.fetchCollectionData(
|
||||||
);
|
userCollections[0].id,
|
||||||
|
);
|
||||||
|
if (E.isRight(collectionData)) {
|
||||||
|
this.pubsub.publish(
|
||||||
|
`user_coll/${userID}/duplicated`,
|
||||||
|
collectionData.right,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
userCollections.forEach((collection) =>
|
||||||
|
this.pubsub.publish(
|
||||||
|
`user_coll/${userID}/created`,
|
||||||
|
this.cast(collection),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return E.right(true);
|
return E.right(true);
|
||||||
}
|
}
|
||||||
@@ -1182,9 +1199,66 @@ export class UserCollectionService {
|
|||||||
userID,
|
userID,
|
||||||
collection.right.parentID,
|
collection.right.parentID,
|
||||||
reqType,
|
reqType,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
if (E.isLeft(result)) return E.left(result.left as string);
|
if (E.isLeft(result)) return E.left(result.left as string);
|
||||||
|
|
||||||
return E.right(true);
|
return E.right(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a JSON containing all the contents of a collection
|
||||||
|
*
|
||||||
|
* @param collection Collection whose details we want to fetch
|
||||||
|
* @returns A JSON string containing all the contents of a collection
|
||||||
|
*/
|
||||||
|
private async fetchCollectionData(
|
||||||
|
collectionID: string,
|
||||||
|
): Promise<E.Left<string> | E.Right<UserCollectionDuplicatedData>> {
|
||||||
|
const collection = await this.getUserCollection(collectionID);
|
||||||
|
if (E.isLeft(collection)) return E.left(collection.left);
|
||||||
|
|
||||||
|
const { id, title, data, type, parentID, userUid } = collection.right;
|
||||||
|
const orderIndex = 'asc';
|
||||||
|
|
||||||
|
const [childCollections, requests] = await Promise.all([
|
||||||
|
this.prisma.userCollection.findMany({
|
||||||
|
where: { parentID: id },
|
||||||
|
orderBy: { orderIndex },
|
||||||
|
}),
|
||||||
|
this.prisma.userRequest.findMany({
|
||||||
|
where: { collectionID: id },
|
||||||
|
orderBy: { orderIndex },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const childCollectionDataList = await Promise.all(
|
||||||
|
childCollections.map(({ id }) => this.fetchCollectionData(id)),
|
||||||
|
);
|
||||||
|
|
||||||
|
const failedChildData = childCollectionDataList.find(E.isLeft);
|
||||||
|
if (failedChildData) return E.left(failedChildData.left);
|
||||||
|
|
||||||
|
const childCollectionsJSONStr = JSON.stringify(
|
||||||
|
(childCollectionDataList as E.Right<UserCollectionDuplicatedData>[]).map(
|
||||||
|
(childCollection) => childCollection.right,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
const transformedRequests = requests.map((requestObj) => ({
|
||||||
|
...requestObj,
|
||||||
|
request: JSON.stringify(requestObj.request),
|
||||||
|
}));
|
||||||
|
|
||||||
|
return E.right(<UserCollectionDuplicatedData>{
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
data,
|
||||||
|
type,
|
||||||
|
parentID,
|
||||||
|
userID: userUid,
|
||||||
|
childCollections: childCollectionsJSONStr,
|
||||||
|
requests: transformedRequests,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { ObjectType, Field, ID, registerEnumType } from '@nestjs/graphql';
|
import { ObjectType, Field, ID, registerEnumType } from '@nestjs/graphql';
|
||||||
|
import { User } from '@prisma/client';
|
||||||
import { ReqType } from 'src/types/RequestTypes';
|
import { ReqType } from 'src/types/RequestTypes';
|
||||||
|
import { UserRequest } from 'src/user-request/user-request.model';
|
||||||
|
|
||||||
@ObjectType()
|
@ObjectType()
|
||||||
export class UserCollection {
|
export class UserCollection {
|
||||||
@@ -73,3 +75,52 @@ export class UserCollectionExportJSONData {
|
|||||||
})
|
})
|
||||||
collectionType: ReqType;
|
collectionType: ReqType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ObjectType()
|
||||||
|
export class UserCollectionDuplicatedData {
|
||||||
|
@Field(() => ID, {
|
||||||
|
description: 'ID of the user collection',
|
||||||
|
})
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@Field({
|
||||||
|
description: 'Displayed title of the user collection',
|
||||||
|
})
|
||||||
|
title: string;
|
||||||
|
|
||||||
|
@Field({
|
||||||
|
description: 'JSON string representing the collection data',
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
data: string;
|
||||||
|
|
||||||
|
@Field(() => ReqType, {
|
||||||
|
description: 'Type of the user collection',
|
||||||
|
})
|
||||||
|
type: ReqType;
|
||||||
|
|
||||||
|
@Field({
|
||||||
|
description: 'Parent ID of the duplicated User Collection',
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
parentID: string | null;
|
||||||
|
|
||||||
|
@Field({
|
||||||
|
description: 'User ID of the duplicated User Collection',
|
||||||
|
})
|
||||||
|
userID: string;
|
||||||
|
|
||||||
|
@Field({
|
||||||
|
description: 'Child collections of the duplicated User Collection',
|
||||||
|
})
|
||||||
|
childCollections: string;
|
||||||
|
|
||||||
|
@Field(() => [UserRequest], {
|
||||||
|
description: 'Requests of the duplicated User Collection',
|
||||||
|
})
|
||||||
|
requests: UserRequest[];
|
||||||
|
}
|
||||||
|
|
||||||
|
registerEnumType(ReqType, {
|
||||||
|
name: 'CollType',
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user