Start to refactor User<->EmailAddress

This commit is contained in:
Robert Schäfer 2019-07-14 11:32:19 +02:00
parent 904142cf6e
commit 46589362fa
4 changed files with 14 additions and 7 deletions

View File

@ -4,7 +4,6 @@ module.exports = {
id: { type: 'string', primary: true, default: uuid }, // TODO: should be type: 'uuid' but simplified for our tests
actorId: { type: 'string', allow: [null] },
name: { type: 'string', min: 3 },
email: { type: 'string', lowercase: true, email: true },
slug: 'string',
encryptedPassword: 'string',
avatar: { type: 'string', allow: [null] },

View File

@ -21,8 +21,8 @@ export default {
// }
const session = driver.session()
const result = await session.run(
'MATCH (user:User {email: $userEmail}) ' +
'RETURN user {.id, .slug, .name, .avatar, .email, .encryptedPassword, .role, .disabled} as user LIMIT 1',
'MATCH (user:User)-[:PRIMARY_EMAIL]->(e:EmailAddress {email: $userEmail})' +
'RETURN user {.id, .slug, .name, .avatar, .encryptedPassword, .role, .disabled} as user LIMIT 1',
{
userEmail: email,
},
@ -48,7 +48,7 @@ export default {
changePassword: async (_, { oldPassword, newPassword }, { driver, user }) => {
const session = driver.session()
let result = await session.run(
`MATCH (user:User {email: $userEmail})
`MATCH (user:User)-[:PRIMARY_EMAIL]->(e:EmailAddress {email: $userEmail})
RETURN user {.id, .email, .encryptedPassword}`,
{
userEmail: user.email,
@ -68,7 +68,7 @@ export default {
} else {
const newEncryptedPassword = await bcrypt.hashSync(newPassword, 10)
session.run(
`MATCH (user:User {email: $userEmail})
`MATCH (user:User)-[:PRIMARY_EMAIL]->(e:EmailAddress {email: $userEmail})
SET user.encryptedPassword = $newEncryptedPassword
RETURN user
`,

View File

@ -2,7 +2,7 @@ type User {
id: ID!
actorId: String
name: String
email: String!
email: String! @cypher(statement: "MATCH (this)-[:PRIMARY_EMAIL]->(e:EmailAddress) RETURN e.email")
slug: String!
avatar: String
coverImg: String

View File

@ -21,7 +21,15 @@ export default function create() {
...args,
}
args = await encryptPassword(args)
return neodeInstance.create('User', args)
const [user, email] = await Promise.all([
neodeInstance.create('User', args),
neodeInstance.create('EmailAddress', { email: args.email }),
])
await Promise.all([
user.relateTo(email, 'primaryEmail'),
email.relateTo(user, 'belongsTo')
])
return user
},
}
}