/
create.js
executable file
·85 lines (80 loc) · 1.65 KB
/
create.js
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
const Errors = {
EMAIL_ALREADY_IN_USE: {
emailAlreadyInUse: 'Email already in use',
},
USERNAME_ALREADY_IN_USE: {
usernameAlreadyInUse: 'Username already in use',
},
};
module.exports = {
inputs: {
email: {
type: 'string',
isEmail: true,
required: true,
},
password: {
type: 'string',
minLength: 6,
regex: /^(?=.*[A-Za-z])(?=.*\d).+$/,
required: true,
},
name: {
type: 'string',
required: true,
},
username: {
type: 'string',
isNotEmptyString: true,
minLength: 3,
maxLength: 16,
regex: /^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/,
allowNull: true,
},
phone: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
organization: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
language: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
subscribeToOwnCards: {
type: 'boolean',
},
},
exits: {
emailAlreadyInUse: {
responseType: 'conflict',
},
usernameAlreadyInUse: {
responseType: 'conflict',
},
},
async fn(inputs) {
const values = _.pick(inputs, [
'email',
'password',
'name',
'username',
'phone',
'organization',
'language',
'subscribeToOwnCards',
]);
const user = await sails.helpers.users
.createOne(values, this.req)
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE);
return {
item: user,
};
},
};