-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUser.helpers.ts
105 lines (98 loc) · 3.41 KB
/
User.helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { errors as FaunaDBErrors, query as q } from 'faunadb'
import { CustomError, ErrorType } from '../errors/CustomError'
import { Either, Left, Right } from '../utils/Either'
import { Just, Maybe, Nothing } from '../utils/Maybe'
import { ExtractType } from '../utils/types'
import { FaunaDBQueryResponse, getDBClient } from './db'
export interface User {
_id: string
email: string
password: string
firstName: string
lastName: string
createdAt: string
updatedAt: string
deletedAt?: string
lastLoggedInAt?: string
lastLoggedOutAt?: string
}
export async function updateUser(
user: Omit<User, 'deletedAt' | 'lastLoggedInAt' | 'lastLoggedOutAt'> & {
deletedAt?: string | null // null signifies deleting this key from object in db
lastLoggedInAt?: string | null // null signifies deleting this key from object in db
lastLoggedOutAt?: string | null // null signifies deleting this key from object in db
},
dbReference: string
): Promise<Either<CustomError, User>> {
// tslint:disable-next-line:no-console
console.log(
`Updating user with id ${user._id}${
process.env.NODE_ENV === 'production'
? '.'
: ' -> ' + JSON.stringify(user, null, 2) + ' REF:' + dbReference
}`
)
try {
const updatedAt = new Date().toISOString()
const db = getDBClient()
const { data: updatedUser } = (await db.query(
q.Update(q.Ref(q.Collection(process.env.JWT_EXAMPLE_DB_USER_CLASS_NAME!), dbReference), {
data: { ...user, updatedAt }
})
)) as FaunaDBQueryResponse<User>
// tslint:disable-next-line:no-console
console.log(
`User with id ${updatedUser._id} updated${
process.env.NODE_ENV === 'production' ? '.' : ' -> ' + JSON.stringify(updatedUser, null, 2)
}`
)
return Right<CustomError, User>(updatedUser)
} catch (err) {
return Left<CustomError, User>(
new CustomError({
type: ErrorType.InternalServerError,
cause: err
})
)
}
}
interface GetUserOptions {
fetchEvenDeleted: boolean
}
export async function getUserAndRefByEmail(
email: ExtractType<User, 'email'>,
options: GetUserOptions = { fetchEvenDeleted: false }
): Promise<Either<CustomError, Maybe<[User, string]>>> {
// tslint:disable-next-line:no-console
console.log(
`Fetching user and ref${process.env.NODE_ENV === 'production' ? '.' : ' -> ' + email}`
)
try {
const db = getDBClient()
const {
data: user,
ref: { id: ref }
} = (await db.query(
q.Get(q.Match(q.Index(process.env.JWT_EXAMPLE_DB_USER_BY_EMAIL_INDEX_NAME!), email))
)) as FaunaDBQueryResponse<User>
// tslint:disable-next-line:no-console
console.log(
`User found with id ${user._id}${
process.env.NODE_ENV === 'production' ? '.' : ' -> ' + JSON.stringify(user, null, 2)
}`
)
// if the user is deleted, and we aren't asked to fetch deleted users, return nothing.
if (!options.fetchEvenDeleted && new Date(user.deletedAt || '').getTime()) {
console.log(`User is a deleted user, returning nothing.`) // tslint:disable-line:no-console
return Right(Nothing())
}
return Right(Just([user, ref]))
} catch (err) {
// no user with email found
if (err instanceof FaunaDBErrors.NotFound) {
console.log(`User not found.`) // tslint:disable-line:no-console
return Right(Nothing())
}
return Left(new CustomError({ type: ErrorType.InternalServerError, cause: err }))
}
}