refactor: modifed return types of mutation/subscriptions in UserCollections (#57)

* refactor: modifed userCollectionRemoved subscription to return custom return type

* chore: created new return type for export to JSON mutation in UserCollection

* refactor: added reqType to exportUserCollectionsToJSON query

* chore: remove duplicate enum in user-collection.model.ts file
This commit is contained in:
Balu Babu
2023-03-29 15:50:48 +05:30
committed by GitHub
parent b6950332ad
commit ea93162056
4 changed files with 63 additions and 10 deletions

View File

@@ -19,7 +19,10 @@ import {
import { TeamInvitation } from 'src/team-invitation/team-invitation.model';
import { InvitedUser } from '../admin/invited-user.model';
import { UserCollection } from '@prisma/client';
import { UserCollectionReorderData } from 'src/user-collection/user-collections.model';
import {
UserCollectionRemovedData,
UserCollectionReorderData,
} from 'src/user-collection/user-collections.model';
import { Shortcode } from 'src/shortcode/shortcode.model';
// A custom message type that defines the topic and the corresponding payload.
@@ -42,7 +45,7 @@ export type TopicDef = {
[
topic: `user_coll/${string}/${'created' | 'updated' | 'moved'}`
]: UserCollection;
[topic: `user_coll/${string}/${'deleted'}`]: string;
[topic: `user_coll/${string}/${'deleted'}`]: UserCollectionRemovedData;
[topic: `user_coll/${string}/${'order_updated'}`]: UserCollectionReorderData;
[topic: `team/${string}/member_removed`]: string;
[topic: `team/${string}/${'member_added' | 'member_updated'}`]: TeamMember;

View File

@@ -16,6 +16,8 @@ import { AuthUser } from 'src/types/AuthUser';
import { UserCollectionService } from './user-collection.service';
import {
UserCollection,
UserCollectionExportJSONData,
UserCollectionRemovedData,
UserCollectionReorderData,
} from './user-collections.model';
import { throwErr } from 'src/utils';
@@ -143,7 +145,7 @@ export class UserCollectionResolver {
return userCollection.right;
}
@Query(() => String, {
@Query(() => UserCollectionExportJSONData, {
description:
'Returns the JSON string giving the collections and their contents of a user',
})
@@ -158,11 +160,18 @@ export class UserCollectionResolver {
defaultValue: null,
})
collectionID: string,
@Args({
type: () => ReqType,
name: 'collectionType',
description: 'Type of the user collection',
})
collectionType: ReqType,
) {
const jsonString =
await this.userCollectionService.exportUserCollectionsToJSON(
user.uid,
collectionID,
collectionType,
);
if (E.isLeft(jsonString)) throwErr(jsonString.left as string);
@@ -371,7 +380,7 @@ export class UserCollectionResolver {
return this.pubSub.asyncIterator(`user_coll/${user.uid}/updated`);
}
@Subscription(() => ID, {
@Subscription(() => UserCollectionRemovedData, {
description: 'Listen to when a User Collection has been deleted',
resolve: (value) => value,
})

View File

@@ -19,7 +19,10 @@ import * as E from 'fp-ts/Either';
import * as O from 'fp-ts/Option';
import { PubSubService } from 'src/pubsub/pubsub.service';
import { Prisma, UserCollection, ReqType as DBReqType } from '@prisma/client';
import { UserCollection as UserCollectionModel } from './user-collections.model';
import {
UserCollection as UserCollectionModel,
UserCollectionExportJSONData,
} from './user-collections.model';
import { ReqType } from 'src/types/RequestTypes';
import { isValidLength, stringToJson } from 'src/utils';
import { CollectionFolder } from 'src/types/CollectionFolder';
@@ -419,7 +422,10 @@ export class UserCollectionService {
this.pubsub.publish(
`user_coll/${deletedUserCollection.right.userUid}/deleted`,
deletedUserCollection.right.id,
{
id: deletedUserCollection.right.id,
type: ReqType[deletedUserCollection.right.type],
},
);
return E.right(true);
@@ -852,12 +858,14 @@ export class UserCollectionService {
async exportUserCollectionsToJSON(
userUID: string,
collectionID: string | null,
reqType: ReqType,
) {
// Get all child collections details
const childCollectionList = await this.prisma.userCollection.findMany({
where: {
userUid: userUID,
parentID: collectionID,
type: reqType,
},
});
@@ -879,6 +887,9 @@ export class UserCollectionService {
const parentCollection = await this.getUserCollection(collectionID);
if (E.isLeft(parentCollection)) return E.left(parentCollection.left);
if (parentCollection.right.type !== reqType)
return E.left(USER_COLL_NOT_SAME_TYPE);
// Fetch all child requests that belong to collectionID
const requests = await this.prisma.userRequest.findMany({
where: {
@@ -890,8 +901,8 @@ export class UserCollectionService {
},
});
return E.right(
JSON.stringify({
return E.right(<UserCollectionExportJSONData>{
exportedCollection: JSON.stringify({
id: parentCollection.right.id,
name: parentCollection.right.title,
folders: collectionListObjects,
@@ -903,10 +914,14 @@ export class UserCollectionService {
};
}),
}),
);
collectionType: parentCollection.right.type,
});
}
return E.right(JSON.stringify(collectionListObjects));
return E.right(<UserCollectionExportJSONData>{
exportedCollection: JSON.stringify(collectionListObjects),
collectionType: reqType,
});
}
/**

View File

@@ -38,6 +38,32 @@ export class UserCollectionReorderData {
nextUserCollection?: UserCollection;
}
@ObjectType()
export class UserCollectionRemovedData {
@Field(() => ID, {
description: 'ID of User Collection being removed',
})
id: string;
@Field(() => ReqType, {
description: 'Type of the user collection',
})
type: ReqType;
}
registerEnumType(ReqType, {
name: 'CollType',
});
@ObjectType()
export class UserCollectionExportJSONData {
@Field(() => ID, {
description: 'Stringified contents of the collection',
})
exportedCollection: string;
@Field(() => ReqType, {
description: 'Type of the user collection',
})
collectionType: ReqType;
}