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:
@@ -19,7 +19,10 @@ import {
|
|||||||
import { TeamInvitation } from 'src/team-invitation/team-invitation.model';
|
import { TeamInvitation } from 'src/team-invitation/team-invitation.model';
|
||||||
import { InvitedUser } from '../admin/invited-user.model';
|
import { InvitedUser } from '../admin/invited-user.model';
|
||||||
import { UserCollection } from '@prisma/client';
|
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';
|
import { Shortcode } from 'src/shortcode/shortcode.model';
|
||||||
|
|
||||||
// A custom message type that defines the topic and the corresponding payload.
|
// 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'}`
|
topic: `user_coll/${string}/${'created' | 'updated' | 'moved'}`
|
||||||
]: UserCollection;
|
]: UserCollection;
|
||||||
[topic: `user_coll/${string}/${'deleted'}`]: string;
|
[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;
|
||||||
[topic: `team/${string}/${'member_added' | 'member_updated'}`]: TeamMember;
|
[topic: `team/${string}/${'member_added' | 'member_updated'}`]: TeamMember;
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ import { AuthUser } from 'src/types/AuthUser';
|
|||||||
import { UserCollectionService } from './user-collection.service';
|
import { UserCollectionService } from './user-collection.service';
|
||||||
import {
|
import {
|
||||||
UserCollection,
|
UserCollection,
|
||||||
|
UserCollectionExportJSONData,
|
||||||
|
UserCollectionRemovedData,
|
||||||
UserCollectionReorderData,
|
UserCollectionReorderData,
|
||||||
} from './user-collections.model';
|
} from './user-collections.model';
|
||||||
import { throwErr } from 'src/utils';
|
import { throwErr } from 'src/utils';
|
||||||
@@ -143,7 +145,7 @@ export class UserCollectionResolver {
|
|||||||
return userCollection.right;
|
return userCollection.right;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Query(() => String, {
|
@Query(() => UserCollectionExportJSONData, {
|
||||||
description:
|
description:
|
||||||
'Returns the JSON string giving the collections and their contents of a user',
|
'Returns the JSON string giving the collections and their contents of a user',
|
||||||
})
|
})
|
||||||
@@ -158,11 +160,18 @@ export class UserCollectionResolver {
|
|||||||
defaultValue: null,
|
defaultValue: null,
|
||||||
})
|
})
|
||||||
collectionID: string,
|
collectionID: string,
|
||||||
|
@Args({
|
||||||
|
type: () => ReqType,
|
||||||
|
name: 'collectionType',
|
||||||
|
description: 'Type of the user collection',
|
||||||
|
})
|
||||||
|
collectionType: ReqType,
|
||||||
) {
|
) {
|
||||||
const jsonString =
|
const jsonString =
|
||||||
await this.userCollectionService.exportUserCollectionsToJSON(
|
await this.userCollectionService.exportUserCollectionsToJSON(
|
||||||
user.uid,
|
user.uid,
|
||||||
collectionID,
|
collectionID,
|
||||||
|
collectionType,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (E.isLeft(jsonString)) throwErr(jsonString.left as string);
|
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`);
|
return this.pubSub.asyncIterator(`user_coll/${user.uid}/updated`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscription(() => ID, {
|
@Subscription(() => UserCollectionRemovedData, {
|
||||||
description: 'Listen to when a User Collection has been deleted',
|
description: 'Listen to when a User Collection has been deleted',
|
||||||
resolve: (value) => value,
|
resolve: (value) => value,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -19,7 +19,10 @@ import * as E from 'fp-ts/Either';
|
|||||||
import * as O from 'fp-ts/Option';
|
import * as O from 'fp-ts/Option';
|
||||||
import { PubSubService } from 'src/pubsub/pubsub.service';
|
import { PubSubService } from 'src/pubsub/pubsub.service';
|
||||||
import { Prisma, UserCollection, ReqType as DBReqType } from '@prisma/client';
|
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 { ReqType } from 'src/types/RequestTypes';
|
||||||
import { isValidLength, stringToJson } from 'src/utils';
|
import { isValidLength, stringToJson } from 'src/utils';
|
||||||
import { CollectionFolder } from 'src/types/CollectionFolder';
|
import { CollectionFolder } from 'src/types/CollectionFolder';
|
||||||
@@ -419,7 +422,10 @@ export class UserCollectionService {
|
|||||||
|
|
||||||
this.pubsub.publish(
|
this.pubsub.publish(
|
||||||
`user_coll/${deletedUserCollection.right.userUid}/deleted`,
|
`user_coll/${deletedUserCollection.right.userUid}/deleted`,
|
||||||
deletedUserCollection.right.id,
|
{
|
||||||
|
id: deletedUserCollection.right.id,
|
||||||
|
type: ReqType[deletedUserCollection.right.type],
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return E.right(true);
|
return E.right(true);
|
||||||
@@ -852,12 +858,14 @@ export class UserCollectionService {
|
|||||||
async exportUserCollectionsToJSON(
|
async exportUserCollectionsToJSON(
|
||||||
userUID: string,
|
userUID: string,
|
||||||
collectionID: string | null,
|
collectionID: string | null,
|
||||||
|
reqType: ReqType,
|
||||||
) {
|
) {
|
||||||
// Get all child collections details
|
// Get all child collections details
|
||||||
const childCollectionList = await this.prisma.userCollection.findMany({
|
const childCollectionList = await this.prisma.userCollection.findMany({
|
||||||
where: {
|
where: {
|
||||||
userUid: userUID,
|
userUid: userUID,
|
||||||
parentID: collectionID,
|
parentID: collectionID,
|
||||||
|
type: reqType,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -879,6 +887,9 @@ export class UserCollectionService {
|
|||||||
const parentCollection = await this.getUserCollection(collectionID);
|
const parentCollection = await this.getUserCollection(collectionID);
|
||||||
if (E.isLeft(parentCollection)) return E.left(parentCollection.left);
|
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
|
// Fetch all child requests that belong to collectionID
|
||||||
const requests = await this.prisma.userRequest.findMany({
|
const requests = await this.prisma.userRequest.findMany({
|
||||||
where: {
|
where: {
|
||||||
@@ -890,8 +901,8 @@ export class UserCollectionService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return E.right(
|
return E.right(<UserCollectionExportJSONData>{
|
||||||
JSON.stringify({
|
exportedCollection: JSON.stringify({
|
||||||
id: parentCollection.right.id,
|
id: parentCollection.right.id,
|
||||||
name: parentCollection.right.title,
|
name: parentCollection.right.title,
|
||||||
folders: collectionListObjects,
|
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,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -38,6 +38,32 @@ export class UserCollectionReorderData {
|
|||||||
nextUserCollection?: UserCollection;
|
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, {
|
registerEnumType(ReqType, {
|
||||||
name: 'CollType',
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user