12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049 |
- import { Injectable } from '@nestjs/common';
- import {
- USER_COLL_DEST_SAME,
- USER_COLL_IS_PARENT_COLL,
- USER_COLL_NOT_FOUND,
- USER_COLL_NOT_SAME_TYPE,
- USER_COLL_NOT_SAME_USER,
- USER_COLL_REORDERING_FAILED,
- USER_COLL_SAME_NEXT_COLL,
- USER_COLL_SHORT_TITLE,
- USER_COLL_ALREADY_ROOT,
- USER_NOT_FOUND,
- USER_NOT_OWNER,
- USER_COLL_INVALID_JSON,
- } from 'src/errors';
- import { PrismaService } from 'src/prisma/prisma.service';
- import { AuthUser } from 'src/types/AuthUser';
- 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,
- UserCollectionExportJSONData,
- } from './user-collections.model';
- import { ReqType } from 'src/types/RequestTypes';
- import { isValidLength, stringToJson } from 'src/utils';
- import { CollectionFolder } from 'src/types/CollectionFolder';
- @Injectable()
- export class UserCollectionService {
- constructor(
- private readonly prisma: PrismaService,
- private readonly pubsub: PubSubService,
- ) {}
- TITLE_LENGTH = 1;
- /**
- * Typecast a database UserCollection to a UserCollection model
- * @param userCollection database UserCollection
- * @returns UserCollection model
- */
- private cast(collection: UserCollection) {
- return <UserCollectionModel>{
- ...collection,
- userID: collection.userUid,
- };
- }
- /**
- * Returns the count of child collections present for a given collectionID
- * * The count returned is highest OrderIndex + 1
- *
- * @param collectionID The Collection ID
- * @returns Number of Child Collections
- */
- private async getChildCollectionsCount(collectionID: string) {
- const childCollectionCount = await this.prisma.userCollection.findMany({
- where: { parentID: collectionID },
- orderBy: {
- orderIndex: 'desc',
- },
- });
- if (!childCollectionCount.length) return 0;
- return childCollectionCount[0].orderIndex;
- }
- /**
- * Returns the count of root collections present for a given userUID
- * * The count returned is highest OrderIndex + 1
- *
- * @param userID The User UID
- * @returns Number of Root Collections
- */
- private async getRootCollectionsCount(userID: string) {
- const rootCollectionCount = await this.prisma.userCollection.findMany({
- where: { userUid: userID, parentID: null },
- orderBy: {
- orderIndex: 'desc',
- },
- });
- if (!rootCollectionCount.length) return 0;
- return rootCollectionCount[0].orderIndex;
- }
- /**
- * Check to see if Collection belongs to User
- *
- * @param collectionID The collection ID
- * @param userID The User ID
- * @returns An Option of a Boolean
- */
- private async isOwnerCheck(collectionID: string, userID: string) {
- try {
- await this.prisma.userCollection.findFirstOrThrow({
- where: {
- id: collectionID,
- userUid: userID,
- },
- });
- return O.some(true);
- } catch (error) {
- return O.none;
- }
- }
- /**
- * Get User of given Collection ID
- *
- * @param collectionID The collection ID
- * @returns User of given Collection ID
- */
- async getUserOfCollection(collectionID: string) {
- try {
- const userCollection = await this.prisma.userCollection.findUniqueOrThrow(
- {
- where: {
- id: collectionID,
- },
- include: {
- user: true,
- },
- },
- );
- return E.right(userCollection.user);
- } catch (error) {
- return E.left(USER_NOT_FOUND);
- }
- }
- /**
- * Get parent of given Collection ID
- *
- * @param collectionID The collection ID
- * @returns Parent UserCollection of given Collection ID
- */
- async getParentOfUserCollection(collectionID: string) {
- const { parent } = await this.prisma.userCollection.findUnique({
- where: {
- id: collectionID,
- },
- include: {
- parent: true,
- },
- });
- return parent;
- }
- /**
- * Get child collections of given Collection ID
- *
- * @param collectionID The collection ID
- * @param cursor collectionID for pagination
- * @param take Number of items we want returned
- * @param type Type of UserCollection
- * @returns A list of child collections
- */
- async getChildrenOfUserCollection(
- collectionID: string,
- cursor: string | null,
- take: number,
- type: ReqType,
- ) {
- return this.prisma.userCollection.findMany({
- where: {
- parentID: collectionID,
- type: type,
- },
- orderBy: {
- orderIndex: 'asc',
- },
- take: take, // default: 10
- skip: cursor ? 1 : 0,
- cursor: cursor ? { id: cursor } : undefined,
- });
- }
- /**
- * Get collection details
- *
- * @param collectionID The collection ID
- * @returns An Either of the Collection details
- */
- async getUserCollection(collectionID: string) {
- try {
- const userCollection = await this.prisma.userCollection.findUniqueOrThrow(
- {
- where: {
- id: collectionID,
- },
- },
- );
- return E.right(userCollection);
- } catch (error) {
- return E.left(USER_COLL_NOT_FOUND);
- }
- }
- /**
- * Create a new UserCollection
- *
- * @param user The User object
- * @param title The title of new UserCollection
- * @param parentUserCollectionID The parent collectionID (null if root collection)
- * @param type Type of Collection we want to create (REST/GQL)
- * @returns
- */
- async createUserCollection(
- user: AuthUser,
- title: string,
- parentUserCollectionID: string | null,
- type: ReqType,
- ) {
- const isTitleValid = isValidLength(title, this.TITLE_LENGTH);
- if (!isTitleValid) return E.left(USER_COLL_SHORT_TITLE);
- // If creating a child collection
- if (parentUserCollectionID !== null) {
- const parentCollection = await this.getUserCollection(
- parentUserCollectionID,
- );
- if (E.isLeft(parentCollection)) return E.left(parentCollection.left);
- // Check to see if parentUserCollectionID belongs to this User
- if (parentCollection.right.userUid !== user.uid)
- return E.left(USER_NOT_OWNER);
- // Check to see if parent collection is of the same type of new collection being created
- if (parentCollection.right.type !== type)
- return E.left(USER_COLL_NOT_SAME_TYPE);
- }
- const isParent = parentUserCollectionID
- ? {
- connect: {
- id: parentUserCollectionID,
- },
- }
- : undefined;
- const userCollection = await this.prisma.userCollection.create({
- data: {
- title: title,
- type: type,
- user: {
- connect: {
- uid: user.uid,
- },
- },
- parent: isParent,
- orderIndex: !parentUserCollectionID
- ? (await this.getRootCollectionsCount(user.uid)) + 1
- : (await this.getChildCollectionsCount(parentUserCollectionID)) + 1,
- },
- });
- await this.pubsub.publish(`user_coll/${user.uid}/created`, userCollection);
- return E.right(userCollection);
- }
- /**
- *
- * @param user The User Object
- * @param cursor collectionID for pagination
- * @param take Number of items we want returned
- * @param type Type of UserCollection
- * @returns A list of root UserCollections
- */
- async getUserRootCollections(
- user: AuthUser,
- cursor: string | null,
- take: number,
- type: ReqType,
- ) {
- return this.prisma.userCollection.findMany({
- where: {
- userUid: user.uid,
- parentID: null,
- type: type,
- },
- orderBy: {
- orderIndex: 'asc',
- },
- take: take, // default: 10
- skip: cursor ? 1 : 0,
- cursor: cursor ? { id: cursor } : undefined,
- });
- }
- /**
- *
- * @param user The User Object
- * @param userCollectionID The User UID
- * @param cursor collectionID for pagination
- * @param take Number of items we want returned
- * @param type Type of UserCollection
- * @returns A list of child UserCollections
- */
- async getUserChildCollections(
- user: AuthUser,
- userCollectionID: string,
- cursor: string | null,
- take: number,
- type: ReqType,
- ) {
- return this.prisma.userCollection.findMany({
- where: {
- userUid: user.uid,
- parentID: userCollectionID,
- type: type,
- },
- take: take, // default: 10
- skip: cursor ? 1 : 0,
- cursor: cursor ? { id: cursor } : undefined,
- });
- }
- /**
- * Update the title of a UserCollection
- *
- * @param newTitle The new title of collection
- * @param userCollectionID The Collection Id
- * @param userID The User UID
- * @returns An Either of the updated UserCollection
- */
- async renameUserCollection(
- newTitle: string,
- userCollectionID: string,
- userID: string,
- ) {
- const isTitleValid = isValidLength(newTitle, this.TITLE_LENGTH);
- if (!isTitleValid) return E.left(USER_COLL_SHORT_TITLE);
- // Check to see is the collection belongs to the user
- const isOwner = await this.isOwnerCheck(userCollectionID, userID);
- if (O.isNone(isOwner)) return E.left(USER_NOT_OWNER);
- try {
- const updatedUserCollection = await this.prisma.userCollection.update({
- where: {
- id: userCollectionID,
- },
- data: {
- title: newTitle,
- },
- });
- this.pubsub.publish(
- `user_coll/${updatedUserCollection.userUid}/updated`,
- updatedUserCollection,
- );
- return E.right(updatedUserCollection);
- } catch (error) {
- return E.left(USER_COLL_NOT_FOUND);
- }
- }
- /**
- * Delete a UserCollection from the DB
- *
- * @param collectionID The Collection Id
- * @returns The deleted UserCollection
- */
- private async removeUserCollection(collectionID: string) {
- try {
- const deletedUserCollection = await this.prisma.userCollection.delete({
- where: {
- id: collectionID,
- },
- });
- return E.right(deletedUserCollection);
- } catch (error) {
- return E.left(USER_COLL_NOT_FOUND);
- }
- }
- /**
- * Delete child collection and requests of a UserCollection
- *
- * @param collectionID The Collection Id
- * @returns A Boolean of deletion status
- */
- private async deleteCollectionData(collection: UserCollection) {
- // Get all child collections in collectionID
- const childCollectionList = await this.prisma.userCollection.findMany({
- where: {
- parentID: collection.id,
- },
- });
- // Delete child collections
- await Promise.all(
- childCollectionList.map((coll) =>
- this.deleteUserCollection(coll.id, coll.userUid),
- ),
- );
- // Delete all requests in collectionID
- await this.prisma.userRequest.deleteMany({
- where: {
- collectionID: collection.id,
- },
- });
- // Update orderIndexes in userCollection table for user
- await this.updateOrderIndex(
- collection.parentID,
- { gt: collection.orderIndex },
- { decrement: 1 },
- );
- // Delete collection from UserCollection table
- const deletedUserCollection = await this.removeUserCollection(
- collection.id,
- );
- if (E.isLeft(deletedUserCollection))
- return E.left(deletedUserCollection.left);
- this.pubsub.publish(
- `user_coll/${deletedUserCollection.right.userUid}/deleted`,
- {
- id: deletedUserCollection.right.id,
- type: ReqType[deletedUserCollection.right.type],
- },
- );
- return E.right(true);
- }
- /**
- * Delete a UserCollection
- *
- * @param collectionID The Collection Id
- * @param userID The User UID
- * @returns An Either of Boolean of deletion status
- */
- async deleteUserCollection(collectionID: string, userID: string) {
- // Get collection details of collectionID
- const collection = await this.getUserCollection(collectionID);
- if (E.isLeft(collection)) return E.left(USER_COLL_NOT_FOUND);
- // Check to see is the collection belongs to the user
- if (collection.right.userUid !== userID) return E.left(USER_NOT_OWNER);
- // Delete all child collections and requests in the collection
- const collectionData = await this.deleteCollectionData(collection.right);
- if (E.isLeft(collectionData)) return E.left(collectionData.left);
- return E.right(true);
- }
- /**
- * Change parentID of UserCollection's
- *
- * @param collectionID The collection ID
- * @param parentCollectionID The new parent's collection ID or change to root collection
- * @returns If successful return an Either of true
- */
- private async changeParent(
- collection: UserCollection,
- parentCollectionID: string | null,
- ) {
- try {
- let collectionCount: number;
- if (!parentCollectionID)
- collectionCount = await this.getRootCollectionsCount(
- collection.userUid,
- );
- collectionCount = await this.getChildCollectionsCount(parentCollectionID);
- const updatedCollection = await this.prisma.userCollection.update({
- where: {
- id: collection.id,
- },
- data: {
- // if parentCollectionID == null, collection becomes root collection
- // if parentCollectionID != null, collection becomes child collection
- parentID: parentCollectionID,
- orderIndex: collectionCount + 1,
- },
- });
- return E.right(updatedCollection);
- } catch (error) {
- return E.left(USER_COLL_NOT_FOUND);
- }
- }
- /**
- * Check if collection is parent of destCollection
- *
- * @param collection The ID of collection being moved
- * @param destCollection The ID of collection into which we are moving target collection into
- * @returns An Option of boolean, is parent or not
- */
- private async isParent(
- collection: UserCollection,
- destCollection: UserCollection,
- ): Promise<O.Option<boolean>> {
- // Check if collection and destCollection are same
- if (collection === destCollection) {
- return O.none;
- }
- if (destCollection.parentID !== null) {
- // Check if ID of collection is same as parent of destCollection
- if (destCollection.parentID === collection.id) {
- return O.none;
- }
- // Get collection details of collection one step above in the tree i.e the parent collection
- const parentCollection = await this.getUserCollection(
- destCollection.parentID,
- );
- if (E.isLeft(parentCollection)) {
- return O.none;
- }
- // Call isParent again now with parent collection
- return await this.isParent(collection, parentCollection.right);
- } else {
- return O.some(true);
- }
- }
- /**
- * Update the OrderIndex of all collections in given parentID
- *
- * @param parentID The Parent collectionID
- * @param orderIndexCondition Condition to decide what collections will be updated
- * @param dataCondition Increment/Decrement OrderIndex condition
- * @returns A Collection with updated OrderIndexes
- */
- private async updateOrderIndex(
- parentID: string,
- orderIndexCondition: Prisma.IntFilter,
- dataCondition: Prisma.IntFieldUpdateOperationsInput,
- ) {
- const updatedUserCollection = await this.prisma.userCollection.updateMany({
- where: {
- parentID: parentID,
- orderIndex: orderIndexCondition,
- },
- data: { orderIndex: dataCondition },
- });
- return updatedUserCollection;
- }
- /**
- * Move UserCollection into root or another collection
- *
- * @param userCollectionID The ID of collection being moved
- * @param destCollectionID The ID of collection the target collection is being moved into or move target collection to root
- * @param userID The User UID
- * @returns An Either of the moved UserCollection
- */
- async moveUserCollection(
- userCollectionID: string,
- destCollectionID: string | null,
- userID: string,
- ) {
- // Get collection details of collectionID
- const collection = await this.getUserCollection(userCollectionID);
- if (E.isLeft(collection)) return E.left(USER_COLL_NOT_FOUND);
- // Check to see is the collection belongs to the user
- if (collection.right.userUid !== userID) return E.left(USER_NOT_OWNER);
- // destCollectionID == null i.e move collection to root
- if (!destCollectionID) {
- if (!collection.right.parentID) {
- // collection is a root collection
- // Throw error if collection is already a root collection
- return E.left(USER_COLL_ALREADY_ROOT);
- }
- // Move child collection into root and update orderIndexes for root userCollections
- await this.updateOrderIndex(
- collection.right.parentID,
- { gt: collection.right.orderIndex },
- { decrement: 1 },
- );
- // Change parent from child to root i.e child collection becomes a root collection
- const updatedCollection = await this.changeParent(collection.right, null);
- if (E.isLeft(updatedCollection)) return E.left(updatedCollection.left);
- this.pubsub.publish(
- `user_coll/${collection.right.userUid}/moved`,
- updatedCollection.right,
- );
- return E.right(updatedCollection.right);
- }
- // destCollectionID != null i.e move into another collection
- if (userCollectionID === destCollectionID) {
- // Throw error if collectionID and destCollectionID are the same
- return E.left(USER_COLL_DEST_SAME);
- }
- // Get collection details of destCollectionID
- const destCollection = await this.getUserCollection(destCollectionID);
- if (E.isLeft(destCollection)) return E.left(USER_COLL_NOT_FOUND);
- // Check if collection and destCollection belong to the same collection type
- if (collection.right.type !== destCollection.right.type) {
- return E.left(USER_COLL_NOT_SAME_TYPE);
- }
- // Check if collection and destCollection belong to the same user account
- if (collection.right.userUid !== destCollection.right.userUid) {
- return E.left(USER_COLL_NOT_SAME_USER);
- }
- // Check if collection is present on the parent tree for destCollection
- const checkIfParent = await this.isParent(
- collection.right,
- destCollection.right,
- );
- if (O.isNone(checkIfParent)) {
- return E.left(USER_COLL_IS_PARENT_COLL);
- }
- // Move root/child collection into another child collection and update orderIndexes of the previous parent
- await this.updateOrderIndex(
- collection.right.parentID,
- { gt: collection.right.orderIndex },
- { decrement: 1 },
- );
- // Change parent from null to teamCollection i.e collection becomes a child collection
- const updatedCollection = await this.changeParent(
- collection.right,
- destCollection.right.id,
- );
- if (E.isLeft(updatedCollection)) return E.left(updatedCollection.left);
- this.pubsub.publish(
- `user_coll/${collection.right.userUid}/moved`,
- updatedCollection.right,
- );
- return E.right(updatedCollection.right);
- }
- /**
- * Find the number of child collections present in collectionID
- *
- * @param collectionID The Collection ID
- * @returns Number of collections
- */
- getCollectionCount(collectionID: string): Promise<number> {
- return this.prisma.userCollection.count({
- where: { parentID: collectionID },
- });
- }
- /**
- * Update order of root or child collectionID's
- *
- * @param collectionID The ID of collection being re-ordered
- * @param nextCollectionID The ID of collection that is after the moved collection in its new position
- * @param userID The User UID
- * @returns If successful return an Either of true
- */
- async updateUserCollectionOrder(
- collectionID: string,
- nextCollectionID: string | null,
- userID: string,
- ) {
- // Throw error if collectionID and nextCollectionID are the same
- if (collectionID === nextCollectionID)
- return E.left(USER_COLL_SAME_NEXT_COLL);
- // Get collection details of collectionID
- const collection = await this.getUserCollection(collectionID);
- if (E.isLeft(collection)) return E.left(USER_COLL_NOT_FOUND);
- // Check to see is the collection belongs to the user
- if (collection.right.userUid !== userID) return E.left(USER_NOT_OWNER);
- if (!nextCollectionID) {
- // nextCollectionID == null i.e move collection to the end of the list
- try {
- await this.prisma.$transaction(async (tx) => {
- // Step 1: Decrement orderIndex of all items that come after collection.orderIndex till end of list of items
- await tx.userCollection.updateMany({
- where: {
- parentID: collection.right.parentID,
- orderIndex: {
- gte: collection.right.orderIndex + 1,
- },
- },
- data: {
- orderIndex: { decrement: 1 },
- },
- });
- // Step 2: Update orderIndex of collection to length of list
- const updatedUserCollection = await tx.userCollection.update({
- where: { id: collection.right.id },
- data: {
- orderIndex: await this.getCollectionCount(
- collection.right.parentID,
- ),
- },
- });
- });
- this.pubsub.publish(
- `user_coll/${collection.right.userUid}/order_updated`,
- {
- userCollection: this.cast(collection.right),
- nextUserCollection: null,
- },
- );
- return E.right(true);
- } catch (error) {
- return E.left(USER_COLL_REORDERING_FAILED);
- }
- }
- // nextCollectionID != null i.e move to a certain position
- // Get collection details of nextCollectionID
- const subsequentCollection = await this.getUserCollection(nextCollectionID);
- if (E.isLeft(subsequentCollection)) return E.left(USER_COLL_NOT_FOUND);
- if (collection.right.userUid !== subsequentCollection.right.userUid)
- return E.left(USER_COLL_NOT_SAME_USER);
- // Check if collection and subsequentCollection belong to the same collection type
- if (collection.right.type !== subsequentCollection.right.type) {
- return E.left(USER_COLL_NOT_SAME_TYPE);
- }
- try {
- await this.prisma.$transaction(async (tx) => {
- // Step 1: Determine if we are moving collection up or down the list
- const isMovingUp =
- subsequentCollection.right.orderIndex < collection.right.orderIndex;
- // Step 2: Update OrderIndex of items in list depending on moving up or down
- const updateFrom = isMovingUp
- ? subsequentCollection.right.orderIndex
- : collection.right.orderIndex + 1;
- const updateTo = isMovingUp
- ? collection.right.orderIndex - 1
- : subsequentCollection.right.orderIndex - 1;
- await tx.userCollection.updateMany({
- where: {
- parentID: collection.right.parentID,
- orderIndex: { gte: updateFrom, lte: updateTo },
- },
- data: {
- orderIndex: isMovingUp ? { increment: 1 } : { decrement: 1 },
- },
- });
- // Step 3: Update OrderIndex of collection
- const updatedUserCollection = await tx.userCollection.update({
- where: { id: collection.right.id },
- data: {
- orderIndex: isMovingUp
- ? subsequentCollection.right.orderIndex
- : subsequentCollection.right.orderIndex - 1,
- },
- });
- });
- this.pubsub.publish(
- `user_coll/${collection.right.userUid}/order_updated`,
- {
- userCollection: this.cast(collection.right),
- nextUserCollection: this.cast(subsequentCollection.right),
- },
- );
- return E.right(true);
- } catch (error) {
- return E.left(USER_COLL_REORDERING_FAILED);
- }
- }
- /**
- * Generate a JSON containing all the contents of a collection
- *
- * @param userUID The User UID
- * @param collectionID The Collection ID
- * @returns A JSON string containing all the contents of a collection
- */
- private async exportUserCollectionToJSONObject(
- userUID: string,
- collectionID: string,
- ): Promise<E.Left<string> | E.Right<CollectionFolder>> {
- // Get Collection details
- const collection = await this.getUserCollection(collectionID);
- if (E.isLeft(collection)) return E.left(collection.left);
- // Get all child collections whose parentID === collectionID
- const childCollectionList = await this.prisma.userCollection.findMany({
- where: {
- parentID: collectionID,
- userUid: userUID,
- },
- orderBy: {
- orderIndex: 'asc',
- },
- });
- // Create a list of child collection and request data ready for export
- const childrenCollectionObjects: CollectionFolder[] = [];
- for (const coll of childCollectionList) {
- const result = await this.exportUserCollectionToJSONObject(
- userUID,
- coll.id,
- );
- if (E.isLeft(result)) return E.left(result.left);
- childrenCollectionObjects.push(result.right);
- }
- // Fetch all child requests that belong to collectionID
- const requests = await this.prisma.userRequest.findMany({
- where: {
- userUid: userUID,
- collectionID,
- },
- orderBy: {
- orderIndex: 'asc',
- },
- });
- const result: CollectionFolder = {
- id: collection.right.id,
- name: collection.right.title,
- folders: childrenCollectionObjects,
- requests: requests.map((x) => {
- return {
- id: x.id,
- name: x.title,
- ...(x.request as Record<string, unknown>), // type casting x.request of type Prisma.JSONValue to an object to enable spread
- };
- }),
- };
- return E.right(result);
- }
- /**
- * Generate a JSON containing all the contents of collections and requests of a team
- *
- * @param userUID The User UID
- * @returns A JSON string containing all the contents of collections and requests of a team
- */
- 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,
- },
- orderBy: {
- orderIndex: 'asc',
- },
- });
- // Create a list of child collection and request data ready for export
- const collectionListObjects: CollectionFolder[] = [];
- for (const coll of childCollectionList) {
- const result = await this.exportUserCollectionToJSONObject(
- userUID,
- coll.id,
- );
- if (E.isLeft(result)) return E.left(result.left);
- collectionListObjects.push(result.right);
- }
- // If collectionID is not null, return JSONified data for specific collection
- if (collectionID) {
- // Get Details of collection
- 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: {
- userUid: userUID,
- collectionID: parentCollection.right.id,
- },
- orderBy: {
- orderIndex: 'asc',
- },
- });
- return E.right(<UserCollectionExportJSONData>{
- exportedCollection: JSON.stringify({
- id: parentCollection.right.id,
- name: parentCollection.right.title,
- folders: collectionListObjects,
- requests: requests.map((x) => {
- return {
- id: x.id,
- name: x.title,
- ...(x.request as Record<string, unknown>), // type casting x.request of type Prisma.JSONValue to an object to enable spread
- };
- }),
- }),
- collectionType: parentCollection.right.type,
- });
- }
- return E.right(<UserCollectionExportJSONData>{
- exportedCollection: JSON.stringify(collectionListObjects),
- collectionType: reqType,
- });
- }
- /**
- * Generate a Prisma query object representation of a collection and its child collections and requests
- *
- * @param folder CollectionFolder from client
- * @param userID The User ID
- * @param orderIndex Initial OrderIndex of
- * @param reqType The Type of Collection
- * @returns A Prisma query object to create a collection, its child collections and requests
- */
- private generatePrismaQueryObj(
- folder: CollectionFolder,
- userID: string,
- orderIndex: number,
- reqType: DBReqType,
- ): Prisma.UserCollectionCreateInput {
- return {
- title: folder.name,
- user: {
- connect: {
- uid: userID,
- },
- },
- requests: {
- create: folder.requests.map((r, index) => ({
- title: r.name,
- user: {
- connect: {
- uid: userID,
- },
- },
- type: reqType,
- request: r,
- orderIndex: index + 1,
- })),
- },
- orderIndex: orderIndex,
- type: reqType,
- children: {
- create: folder.folders.map((f, index) =>
- this.generatePrismaQueryObj(f, userID, index + 1, reqType),
- ),
- },
- };
- }
- /**
- * Create new UserCollections and UserRequests from JSON string
- *
- * @param jsonString The JSON string of the content
- * @param userID The User ID
- * @param destCollectionID The Collection ID
- * @param reqType The Type of Collection
- * @returns An Either of a Boolean if the creation operation was successful
- */
- async importCollectionsFromJSON(
- jsonString: string,
- userID: string,
- destCollectionID: string | null,
- reqType: DBReqType,
- ) {
- // Check to see if jsonString is valid
- const collectionsList = stringToJson<CollectionFolder[]>(jsonString);
- if (E.isLeft(collectionsList)) return E.left(USER_COLL_INVALID_JSON);
- // Check to see if parsed jsonString is an array
- if (!Array.isArray(collectionsList.right))
- return E.left(USER_COLL_INVALID_JSON);
- // Check to see if destCollectionID belongs to this User
- if (destCollectionID) {
- const parentCollection = await this.getUserCollection(destCollectionID);
- if (E.isLeft(parentCollection)) return E.left(parentCollection.left);
- // Check to see if parentUserCollectionID belongs to this User
- if (parentCollection.right.userUid !== userID)
- return E.left(USER_NOT_OWNER);
- // Check to see if parent collection is of the same type of new collection being created
- if (parentCollection.right.type !== reqType)
- return E.left(USER_COLL_NOT_SAME_TYPE);
- }
- // Get number of root or child collections for destCollectionID(if destcollectionID != null) or destTeamID(if destcollectionID == null)
- const count = !destCollectionID
- ? await this.getRootCollectionsCount(userID)
- : await this.getChildCollectionsCount(destCollectionID);
- // Generate Prisma Query Object for all child collections in collectionsList
- const queryList = collectionsList.right.map((x) =>
- this.generatePrismaQueryObj(x, userID, count + 1, reqType),
- );
- const parent = destCollectionID
- ? {
- connect: {
- id: destCollectionID,
- },
- }
- : undefined;
- const userCollections = await this.prisma.$transaction(
- queryList.map((x) =>
- this.prisma.userCollection.create({
- data: {
- ...x,
- parent,
- },
- }),
- ),
- );
- userCollections.forEach((x) =>
- this.pubsub.publish(`user_coll/${userID}/created`, x),
- );
- return E.right(true);
- }
- }
|