Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async forgotPassword(
@Args('email') email: string,
@Context('req') req: any
): Promise {
const user = await getMongoRepository(User).findOne({
where: {
'local.email': email,
isVerified: true,
},
})
if (!user) {
throw new ForbiddenError('User not found.')
}
const resetPassToken = await generateResetPassToken(user)
const existedEmail = await this.emailResolver.createEmail({
userId: user._id,
type: Type.FORGOT_PASSWORD,
})
// console.log(existedEmail)
await sendMail(
'forgotPassword',
user,
req,
resetPassToken,
async createSubscription(
@Args('source') source: string,
@Args('ccLast4') ccLast4: string,
@Context('currentUser') currentUser: User
): Promise {
// console.log(source)
if (currentUser.stripeId) {
throw new ForbiddenError('stripeId already existed.')
}
const email = currentUser.local
? currentUser.local.email
: currentUser.google
? currentUser.google.email
: currentUser.facebook.email
const customer = await stripe.customers.create({
email,
source,
plan: STRIPE_PLAN!,
})
// console.log(customer)
const user = await getMongoRepository(User).save(
async changeCreditCard(
@Args('source') source: string,
@Args('ccLast4') ccLast4: string,
@Context('currentUser') currentUser: User
): Promise {
// console.log(source)
if (!currentUser.stripeId || currentUser.type !== UserType.PREMIUM) {
throw new ForbiddenError('User not found.')
}
await stripe.customers.update(currentUser.stripeId, {
source,
})
const updateUser = await getMongoRepository(User).save(
new User({
...currentUser,
ccLast4,
})
)
return updateUser
}
@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 }]
}
})
if (existedUser) {
// Let's merge them?
const updateUser = await getMongoRepository(User).save(
new User({
...input,
local: {
@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 }],
},
})
if (existedUser) {
// Let's merge them?
const updateUser = await getMongoRepository(User).save(
new User({
...input,
local: {
async createStore(@Args('input') input: CreateStoreInput): Promise {
const { name } = input
const store = await getMongoRepository(Store).findOne({ name })
if (store) {
throw new ForbiddenError('Store already existed.')
}
return await getMongoRepository(Store).save(new Store({ ...input }))
}
}
async room(@Args('_id') _id: string): Promise {
const room = await getMongoRepository(Room).findOne({
_id
})
if (!room) {
throw new ForbiddenError('Room not found.')
}
return getMongoRepository(Room).findOne({
_id
})
}
async updateUser(
@Args('_id') _id: string,
@Args('input') input: UpdateUserInput
): Promise {
try {
const { password } = input
const user = await getMongoRepository(User).findOne({ _id })
if (!user) {
throw new ForbiddenError('User not found.')
}
const updateUser = await await getMongoRepository(User).save(
new User({
...user,
...input,
local: {
email: user.local.email,
password: await hashPassword(password),
},
})
)
return updateUser ? true : false
} catch (error) {
throw new ApolloError(error)
async joinRoom(
@Args('_id') _id: string,
@Context('currentUser') currentUser: User
): Promise {
const room = await getMongoRepository(Room).findOne({
_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 joined the room.')
}
room.users = [...room.users, currentUser]
return getMongoRepository(Room).save(room) ? true : false
}
async joinRoom(
@Args('_id') _id: string,
@Context('currentUser') currentUser: User
): Promise {
const room = await getMongoRepository(Room).findOne({
_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 joined the room.')
}
room.users = [...room.users, currentUser]
return getMongoRepository(Room).save(room) ? true : false
}