Last active
December 21, 2019 18:33
-
-
Save Hellomik2002/00ded3e24e57b4dcec6800a19eb02af6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Resolver() | |
export class InstitutionResolver { | |
private institutionsCollection!: Collection; | |
private userCollection!: Collection; | |
private orderCollection!: Collection; | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/explicit-member-accessibility | |
@Query(returns => [Institution]) | |
async showAllInst(): Promise<Institution[]> { | |
if (!this.institutionsCollection) { | |
this.institutionsCollection = await MongoConnection.getInstitutionCollection(); | |
} | |
const allInstits = await this.institutionsCollection.find({}).toArray(); | |
const toSend: Institution[] = plainToClass(Institution, allInstits); | |
toSend.forEach(value => { | |
value.buildRating(); | |
}); | |
return toSend; | |
} | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/explicit-member-accessibility | |
@Mutation(returns => Institution) | |
async createInstitution( | |
@Arg('name') name: string, | |
@Arg('uid') uid: string, | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
@Arg('moderatorIds', type => [String]) moderatorIds: string[], | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
@Arg('imageBackground', type => GraphQLUpload) | |
imageBackground: FileUpload, | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
@Arg('iconImage', type => GraphQLUpload) | |
imageIcon: FileUpload, | |
): Promise<Institution | null> { | |
try { | |
if (!this.institutionsCollection) { | |
this.institutionsCollection = await MongoConnection.getInstitutionCollection(); | |
} | |
if (!this.userCollection) { | |
this.userCollection = await MongoConnection.getUserCollection(); | |
} | |
const was: number | null = await this.institutionsCollection.findOne({ | |
$or: [{ uid }], | |
}); | |
if (was) { | |
throw Error('UID already exists'); | |
} | |
// Image upload start | |
const { | |
createReadStream: createReadStreamBackground, | |
filename: filenameBackground, | |
} = await imageBackground; | |
const fileNameStream = `${filenameBackground}${Date.now().toString()}`; | |
const uploadPath = fs.createWriteStream(path.resolve(PATH_WAY, fileNameStream), { | |
autoClose: true, | |
}); | |
const imageBackgroundUrl: string = await new Promise((res, rej) => | |
createReadStreamBackground() | |
.pipe(uploadPath) | |
.on('error', rej) | |
.on('finish', () => res(fileNameStream)), | |
); | |
// Image upload end | |
// Icon upload start | |
const { | |
createReadStream: createReadStreamIcon, | |
filename: filenameIcon, | |
} = await imageIcon; | |
const fileNameStreamIcon = `${filenameIcon}${Date.now().toString()}i`; | |
const uploadPathIcon = fs.createWriteStream( | |
path.resolve(PATH_WAY, fileNameStreamIcon), | |
{ | |
autoClose: true, | |
}, | |
); | |
const imageIconUrl: string = await new Promise((res, rej) => | |
createReadStreamIcon() | |
.pipe(uploadPathIcon) | |
.on('error', rej) | |
.on('finish', () => res(fileNameStreamIcon)), | |
); | |
// Icon upload end | |
const currentInst = plainToClass(Institution, { | |
name, | |
uid, | |
moderatorIds, | |
imageBackgroundUrl, | |
imageIconUrl, | |
}); | |
const sendInstitution = Object.assign(currentInst, { | |
moderatorIds: currentInst.moderatorIds.map(value => new ObjectId(value)), | |
}); | |
const userData = await this.institutionsCollection.insertOne(sendInstitution); | |
const moderatorId = userData.insertedId as string; | |
await this.userCollection.updateMany( | |
{ | |
_id: { | |
$all: moderatorIds.map(value => new ObjectId(value)), | |
}, | |
}, | |
{ | |
$addToSet: { | |
'moderator.institutionIds': { | |
$each: [new ObjectId(moderatorId)], | |
}, | |
}, | |
}, | |
{ | |
upsert: true, | |
}, | |
); | |
return { | |
_id: moderatorId, | |
...currentInst, | |
} as Institution; | |
} catch (e) { | |
throw e; | |
} | |
} | |
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility | |
@UseMiddleware(isAuthInstitution) | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
@Mutation(returns => Institution) | |
async addDataInstitution( | |
@Arg('token') token: string, | |
@Arg('institutionId') institutionId: string, | |
@Ctx() ctx1: UserContext, | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
@Arg('newName', type => String, { nullable: true }) | |
newName?: string, | |
@Arg('addModerators', type => [String], { nullable: true }) | |
newModeratorIds: string[] = [], | |
@Arg('types', type => [String], { nullable: true }) // | |
newTypes: string[] = [], | |
@Arg('executorIds', type => [String], { nullable: true }) | |
newExecutorIds: string[] = [], | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
@Arg('imageBackground', type => GraphQLUpload, { nullable: true }) | |
newImageBackground?: FileUpload, | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
@Arg('iconImage', type => GraphQLUpload, { nullable: true }) | |
newImageIcon?: FileUpload, | |
): Promise<Institution | null> { | |
try { | |
if (!this.institutionsCollection) { | |
this.institutionsCollection = await MongoConnection.getInstitutionCollection(); | |
} | |
if (!this.userCollection) { | |
this.userCollection = await MongoConnection.getUserCollection(); | |
} | |
// start get institution from Db | |
const institutionCurrent = await this.institutionsCollection.findOne( | |
{ | |
_id: new ObjectId(institutionId), | |
}, | |
{ projection: { imageBackgroundUrl: 1, imageIconUrl: 1 } }, | |
); | |
// got urls | |
const addImageBackgroundUrl = newImageBackground | |
? await new Promise(async res => { | |
const backImgPath = institutionCurrent.imageBackgroundUrl; | |
console.log(path.resolve(PATH_WAY, backImgPath)); | |
if (backImgPath && fs.existsSync(path.resolve(PATH_WAY, backImgPath))) { | |
fs.unlink(path.resolve(PATH_WAY, backImgPath), () => {}); | |
} | |
const { | |
createReadStream: createReadStreamBackground, | |
filename: filenameBackground, | |
} = await newImageBackground; | |
const fileNameStream = `${filenameBackground}${Date.now().toString()}`; | |
const uploadPath = fs.createWriteStream( | |
path.resolve(PATH_WAY, fileNameStream), | |
{ autoClose: true }, | |
); | |
const imageBackgroundUrl: string = await new Promise((res, rej) => | |
createReadStreamBackground() | |
.pipe(uploadPath) | |
.on('error', rej) | |
.on('finish', () => res(fileNameStream)), | |
); | |
res({ imageBackgroundUrl }); | |
}) | |
: {}; | |
const addImageIconUrl = newImageIcon | |
? await new Promise(async res => { | |
const backImgPath = institutionCurrent.imageIconUrl; | |
if (backImgPath && fs.existsSync(path.resolve(PATH_WAY, backImgPath))) { | |
fs.unlink(path.resolve(PATH_WAY, backImgPath), () => {}); | |
} | |
const { | |
createReadStream: createReadStreamBackground, | |
filename: filenameBackground, | |
} = await newImageIcon; | |
const fileNameStream = `${filenameBackground}${Date.now().toString()}i`; | |
const uploadPath = fs.createWriteStream( | |
path.resolve(PATH_WAY, fileNameStream), | |
{ autoClose: true }, | |
); | |
const imageIconUrl: string = await new Promise((res, rej) => | |
createReadStreamBackground() | |
.pipe(uploadPath) | |
.on('error', rej) | |
.on('finish', () => res(fileNameStream)), | |
); | |
res({ imageIconUrl }); | |
}) | |
: {}; | |
const addName = newName ? { name: newName } : {}; | |
const addModeratorIds = | |
newModeratorIds.length == 0 | |
? {} | |
: { | |
moderatorIds: { | |
$each: newModeratorIds.map(value => new ObjectId(value)), | |
}, | |
}; | |
const addNewTypes = newTypes.length == 0 ? {} : { executorIds: { $each: newTypes } }; | |
const addExecutorIds = | |
newExecutorIds.length == 0 | |
? {} | |
: { executorIds: { $each: newExecutorIds.map(value => new ObjectId(value)) } }; | |
const sendData = { | |
$set: { | |
...addModeratorIds, | |
...addNewTypes, | |
...addExecutorIds, | |
...addImageBackgroundUrl, | |
...addImageIconUrl, | |
...addName, | |
}, | |
}; | |
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | |
// @ts-ignore | |
if (0 == Object.keys(sendData.$set).length) { | |
throw Error('Nothing'); | |
} | |
const updatedDoc = await this.institutionsCollection.findOneAndUpdate( | |
{ | |
_id: new ObjectId(institutionId), | |
}, | |
sendData, | |
{ returnOriginal: false }, | |
); | |
return updatedDoc.value as Institution; | |
} catch (e) { | |
throw e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment