Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// validate input
const schemas = yup.object().shape({
email: emailSchema.required(),
shortId: contestShortIdSchema.required(),
name: contestNameSchema.required(),
shortDescription: contestShortDescriptionSchema.required(),
description: contestDescriptionSchema.required(),
startTime: startTimeSchema.required(),
finishTime: finishTimeSchema.required(),
permittedLanguageIds: permittedLanguagesSchema.required(),
})
try {
args = await schemas.validate(args)
} catch (error) {
if (error instanceof yup.ValidationError) {
throw new ApolloError('Invalid Input', 'INVALID_INPUT', error.errors)
}
}
// check taken shortId
try {
await contestStore.getContestByShortId(args.shortId)
throw new ApolloError(
'Contest ID Already Taken',
'CONTEST_ID_ALREADY_TAKEN'
)
} catch (error) {
if (!(error instanceof NoSuchContest)) throw error
}
// check languages ids
const availableLangs = await languageStore.getAvailableLanguages()
lastName: profile.name.familyName,
gender: profile.gender === '' && Gender.UNKNOWN,
avatar: profile.photos[0].value
})
)
}
return await tradeToken(user)
}
if (info) {
// console.log(info)
const { code } = info
switch (code) {
case 'ETIMEDOUT':
throw new ApolloError('Failed to reach Google: Try Again')
default:
throw new ApolloError('Something went wrong')
}
}
}
async contest(@Arg('id', { nullable: false }) id: number): Promise {
const contest = await ContestEntity.getRepository().findOne(id)
if (contest) return contest
throw new ApolloError('NoSuchContest', 'No Such Contest')
}
async dishesByShop(@Args('shopId') shopId: string): Promise {
try {
return await this.dishRepository.find({
where: { shopId, isActive: true },
order: { createdAt: 'DESC' }
})
} catch (error) {
throw new ApolloError(error)
}
}
async dish(@Args('id') id: string): Promise {
try {
return await this.dishRepository.findOne({ _id: id })
} catch (error) {
throw new ApolloError(error)
}
}
async shop(@Args('id') id: string): Promise {
try {
return await this.shopRepository.findOne({ _id: id })
} catch (error) {
throw new ApolloError(error)
}
}
try {
const message = 'Not Found: User'
const code = '404'
const additionalProperties = {}
const user = await this.userRepository.findOne({ _id })
if (!user) {
throw new ApolloError(message, code, additionalProperties)
}
user.isActive = false
return (await this.userRepository.save(user)) ? true : false
} catch (error) {
throw new ApolloError(error, '500', {})
}
}
async updateUser(
@Args('_id') _id: string,
@Args('input') input: UpdateUserInput
): Promise {
try {
const { firstName, lastName, password, gender, sites } = input
const user = await this.userRepository.findOne({ _id })
if (!user) {
throw new ApolloError('Not Found: User', '404', {})
}
sites.map(async item => {
const { siteId, permissions } = item
const existedUserPermission = await getMongoRepository(
UserPermission
).findOne({
userId: user._id,
siteId
})
if (existedUserPermission) {
existedUserPermission.permissions = permissions
return getMongoRepository(UserPermission).save(existedUserPermission)
const activeAdditionalProperties = {}
if (!user.isActive) {
throw new ApolloError(
activeMessage,
activeCode,
activeAdditionalProperties
)
}
const lockedMessage = 'Locked'
const lockedCode = '423'
const lockedAdditionalProperties = {}
if (user.isLocked) {
throw new ApolloError(
lockedMessage,
lockedCode,
lockedAdditionalProperties
)
}
const token = jwt.sign(
{
issuer: 'http://lunchapp2.dev.io',
subject: user._id,
audience: user.username
},
process.env.SECRET_KEY,
{
expiresIn: '30d'
}
async getMenuPublishBySite(siteId: string): Promise<menu> {
try {
return await this.commonService.findOneAdapter(Menu, {
siteId,
isPublished: true,
isActive: true
})
} catch (error) {
throw new ApolloError(error)
}
}
</menu>