Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async search(@Args('conditions') conditions: SearchInput): Promise {
let result
const { select, where, order, skip, take } = conditions
if (Object.keys(where).length > 1) {
throw new UserInputError('Your where must be 1 collection.')
}
const type = Object.keys(where)[0]
// const createdAt = { $gte: 0, $lte: new Date().getTime() }
result = await getMongoRepository(type).find({
where: where[type] && JSON.parse(JSON.stringify(where[type])),
order: order && JSON.parse(JSON.stringify(order)),
skip,
take,
})
// console.log(result)
if (result.length === 0) {
order: {
createdAt: 'DESC',
},
});
if (!doc && key === 'documentId') {
doc = await documentRepository.findOne({
where: { documentId: args.id, deletedAt: IsNull() },
order: {
createdAt: 'DESC',
},
});
}
if (!doc) {
throw new UserInputError('Document not found');
}
const data = await payload.documentTransformer.transformInput(
args.input,
payload.schema,
payload.fields
);
if (args.merge) {
defaultsDeep(
data,
await payload.documentTransformer.transformInput(doc.data, payload.schema, payload.fields)
);
}
// @todo userId
async transform(value: any, { metatype }: ArgumentMetadata) {
// destructuring metadata
if (!metatype || !this.toValidate(metatype)) {
return value
}
const object = plainToClass(metatype, value)
const errors = await validate(object)
if (errors.length > 0) {
throw new UserInputError(
`Form Arguments invalid: ${this.formatErrors(errors)}`
)
}
return value
}
async searchUser(@Args('userIds') userIds: string[]): Promise {
let result
if (userIds.length === 0) {
throw new UserInputError('userIds can not be blank.')
}
result = await getMongoRepository(User).find({
where: {
_id: { $in: userIds },
},
})
// tslint:disable-next-line:prefer-conditional-expression
if (result.length > 1) {
result = { users: result }
} else {
result = result[0]
}
return result
async searchUser(@Args('userIds') userIds: string[]): Promise {
let result
if (userIds.length === 0) {
throw new UserInputError('userIds can not be blank.')
}
result = await getMongoRepository(User).find({
where: {
_id: { $in: userIds }
}
})
// tslint:disable-next-line:prefer-conditional-expression
if (result.length > 1) {
result = { users: result }
} else {
result = result[0]
}
return result
const formatError = jest.fn(error => {
expect(error instanceof Error).toBe(true);
expect(error.extensions.code).toEqual('INTERNAL_SERVER_ERROR');
expect(error.extensions.exception.name).toEqual('ValidationError');
expect(error.extensions.exception.message).toBeDefined();
const inputError = new UserInputError('User Input Error');
return {
message: inputError.message,
extensions: inputError.extensions,
};
});
) AS "table"
WHERE "table"."entryId" = ${sequelize.escape(cursor)}
AND "table"."deletedAt" IS NULL
`;
const result = await sequelize.query(sql);
if (result[0] && result[0][0]) {
const index = Number(result[0][0].index);
debug('cursor index: %o', index);
if (args.after) {
findAllPaging.offset = index;
} else {
findAllPaging.offset = Math.max(0, index - (args.first || defaultLimit));
}
} else {
debug(result);
throw new UserInputError('Cursor out of date');
}
}
if (args.first) {
if (args.skip) {
findAllPaging.offset = args.skip;
}
findAllPaging.limit = args.first;
}
findAllPaging.offset = findAllPaging.offset || 0;
findAllPaging.limit = findAllPaging.limit || defaultLimit;
const entries = await ContentEntry.findAll({
...findAllOptions,
...findAllPaging,
> = async (root, args, context) => {
const { id: userId, username, usernameIdentifier, email } = args;
let user: core.types.Nullable = null;
if (userId) {
user = await context.loaders.userById.load(userId);
} else if (username && usernameIdentifier) {
user = await context.loaders.userByUniqueUsername.load({
username,
usernameIdentifier,
});
} else if (email) {
user = await context.loaders.userByEmail.load(email);
} else {
throw new UserInputError(
'must provide user id, username and username identifier, or email',
);
}
if (!user) {
throw new NotFoundError({ entity: NotFoundableEntity.User });
}
return user;
};
async createUser(
@Args('createUserInput') createUserInput: CreateUserInput,
): Promise {
let createdUser: User | undefined;
try {
createdUser = await this.usersService.create(createUserInput);
} catch (error) {
throw new UserInputError(error.message);
}
return createdUser;
}
async resetPassword(
@Args('username') username: string,
@Args('code') code: string,
@Args('password') password: string,
): Promise {
const user = await this.usersService.resetPassword(
username,
code,
password,
);
if (!user) throw new UserInputError('The password was not reset');
return user;
}