Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async verifyEmail(@Args('emailToken') emailToken: string): Promise {
const user = await verifyEmailToken(emailToken)
// console.log(user)
if (!user.isVerified) {
const updateUser = await getMongoRepository(User).save(
new User({
...user,
isVerified: true,
})
)
return updateUser ? true : false
} else {
throw new ForbiddenError('Your email has been verified.')
}
}
async changePassword(
@Args('_id') _id: string,
@Args('currentPassword') currentPassword: string,
@Args('password') password: string
): Promise {
const user = await getMongoRepository(User).findOne({ _id })
// console.log(currentPassword , password)
if (!user) {
throw new ForbiddenError('User not found.')
}
if (!(await comparePassword(currentPassword, user.local.password))) {
throw new ForbiddenError('Your current password is missing or incorrect.')
}
if (await comparePassword(password, user.local.password)) {
throw new ForbiddenError(
'Your new password must be different from your previous password.'
)
}
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 createUser(
@Args('input') input: CreateUserInput,
@Context('pubsub') pubsub: any,
@Context('req') req: any
): Promise {
try {
const { email, password } = input
let existedUser
existedUser = await getMongoRepository(User).findOne({
where: {
'local.email': email
}
})
if (existedUser) {
throw new ForbiddenError('User already exists.')
}
// Is there a Google account with the same email?
existedUser = await getMongoRepository(User).findOne({
where: {
$or: [{ 'google.email': email }, { 'facebook.email': email }]
}
})
async updateTree(
@Args('input') input: string,
@Args('_id') _id: string
): Promise {
try {
const tree = await getMongoRepository(TreeEntity).findOne({ _id })
if (!tree) {
throw new ForbiddenError('Tree not found.')
}
const newTree = {
...tree,
treeData: input
}
const result = await getMongoRepository(TreeEntity).save(
new TreeEntity({ ...newTree })
)
return true
} catch (err) {
throw new Error(err)
}
}
}
async createCompany(
@Args('input') input: CreateCompanyInput
): Promise {
const { name } = input
const company = await getMongoRepository(Company).findOne({ name })
if (company) {
throw new ForbiddenError('Company already existed.')
}
return await getMongoRepository(Company).save(new Company({ ...input }))
}
}
_id,
})
if (!room) {
throw new ForbiddenError('Room not found.')
}
const rs = room.users.filter(item => item._id === currentUser._id)
if (rs.length === 0) {
throw new ForbiddenError('You are not in the room.')
}
room.users = room.users.filter(item => item._id !== currentUser._id)
return getMongoRepository(Room).save(room) ? true : false
}
}
async roles(): Promise {
return getMongoRepository(Role).find({
cache: true
})
}
constructor() {
this.repository = getMongoRepository(Session)
}
onDisconnect: async (webSocket, context) => {
NODE_ENV !== 'production' &&
Logger.error(`❌ Disconnected to websocket`, '', 'GraphQL', false)
const { initPromise } = context
const { currentUser } = await initPromise
await getMongoRepository(User).updateOne(
{ _id: currentUser._id },
{
$set: { isOnline: false },
},
{
upsert: true,
}
)
},
},