Skip to content

Commit 0880e1d

Browse files
committed
move validation to extra file
1 parent b39b0fe commit 0880e1d

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

packages/models/src/user/index.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
'use strict';
22

33
const { v4: uuidv4 } = require('uuid');
4-
const invariant = require('tiny-invariant');
54

65
const { userTable, passwordTable, profileTable } = require('../../schema/schema');
76
const { db } = require('../drizzle');
8-
const { createProfile } = require('../profile/profile');
97
const { eq } = require('drizzle-orm');
108
const ModelError = require('../modelError');
119
const { checkPassword, validatePassword, passwordHasher } = require('../password/utils');
1210
const IsEmail = require('isemail');
11+
const { validateField } = require('../utils/validation');
1312

1413
const userNameRequirementsText = 'Parameter name must consist of at least 3 and up to 40 alphanumerics (a-zA-Z0-9), dot (.), dash (-), underscore (_) and spaces.';
1514
const nameValidRegex =
1615
/^[^~`!@#$%^&*()+=£{}[\]|\\:;"'<>,?/\n\r\t\s][^~`!@#$%^&*()+=£{}[\]|\\:;"'<>,?/\n\r\t]{1,39}[^~`!@#$%^&*()+=£{}[\]|\\:;"'<>,?/\n\r\t\s]$/;
1716

18-
const validateField = function validateField (field, expr, msg) {
19-
try {
20-
invariant(expr, msg);
21-
} catch (error) {
22-
const err = new Error(msg);
23-
err.name = 'ValidationError';
24-
err.errors = {
25-
[field]: { message: msg }
26-
};
27-
throw err;
28-
}
29-
};
30-
3117
const findUserByNameOrEmail = async function findUserByNameOrEmail (
3218
emailOrName
3319
) {
@@ -69,30 +55,44 @@ const createUser = async function createUser (name, email, password, language) {
6955

7056
const hashedPassword = await passwordHasher(password);
7157

72-
const user = await db.transaction(async (tx) => {
73-
const user = await tx
74-
.insert(userTable)
75-
.values({ name, email, language })
76-
.returning();
77-
78-
await tx.insert(passwordTable).values({
79-
hash: hashedPassword,
80-
userId: user[0].id
81-
});
82-
83-
await tx.insert(profileTable).values({
84-
username: name,
85-
public: false,
86-
userId: user[0].id
58+
try {
59+
const user = await db.transaction(async (tx) => {
60+
const user = await tx
61+
.insert(userTable)
62+
.values({ name, email, language })
63+
.returning();
64+
65+
await tx.insert(passwordTable).values({
66+
hash: hashedPassword,
67+
userId: user[0].id
68+
});
69+
70+
await tx.insert(profileTable).values({
71+
username: name,
72+
public: false,
73+
userId: user[0].id
74+
});
75+
76+
return user[0];
8777
});
8878

89-
return user[0];
90-
});
91-
92-
// Delete not needed properties
93-
delete user.emailConfirmationToken;
79+
// Delete not needed properties
80+
delete user.emailConfirmationToken;
9481

95-
return user;
82+
return user;
83+
} catch (error) {
84+
// Catch and transform database errors
85+
console.log(error);
86+
/**
87+
* {
88+
"code": "BadRequest",
89+
"message": "Duplicate user detected"
90+
}
91+
*/
92+
if (error.code === '23505') {
93+
throw new ModelError('Duplicate user detected', { type: 'BadRequest' });
94+
}
95+
}
9696
};
9797

9898
const destroyUser = async function destroyUser (user) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const invariant = require('tiny-invariant');
4+
5+
const validateField = function validateField (field, expr, msg) {
6+
try {
7+
invariant(expr, msg);
8+
} catch (error) {
9+
const err = new Error(msg);
10+
err.name = 'ValidationError';
11+
err.errors = {
12+
[field]: { message: msg }
13+
};
14+
throw err;
15+
}
16+
};
17+
18+
module.exports = {
19+
validateField
20+
};

0 commit comments

Comments
 (0)