/
swissdata-user-field.ts
executable file
·239 lines (208 loc) · 7.74 KB
/
swissdata-user-field.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { AppState } from '../../state/index';
import { inject, bindable, NewInstance, computedFrom } from 'aurelia-framework';
import { SwissdataApi } from '../../helpers/swissdata-api';
import { getLogger, Logger } from 'aurelia-logging';
import { DOM } from 'aurelia-pal';
import { jsonify, validate } from '../../deco';
import { UserModel } from '../../models/user.model';
import { notify, errorify, AureliaUXFormRenderer } from 'aurelia-resources';
import { ValidationController, validateTrigger } from 'aurelia-validation';
import { I18N } from 'aurelia-i18n';
import PhoneNumber from 'awesome-phonenumber';
const log = getLogger('swissdata-user-field');
@inject(SwissdataApi, Element, NewInstance.of(ValidationController), I18N)
export class SwissdataUserField {
private log: Logger;
@bindable public instance: UserModel | 'state' = 'state';
@bindable public property: 'email' | 'mobile' | 'password';
private _instance: UserModel;
private currentPassword: string;
private newPassword: string;
private email: string;
private mobile: string; // full mobile value
private token: string;
private code: string;
constructor(private swissdataApi: SwissdataApi, private element: Element, private validationController: ValidationController, private i18n: I18N) {
this.swissdataApi.store.registerAction('updateUser', updateUser);
this.validationController.validateTrigger = validateTrigger.blur;
validate.ValidationRules
.ensure('currentPassword').displayName(this.i18n.tr('userField.Current Password')).required()
.ensure('newPassword').displayName(this.i18n.tr('userField.New Password')).required().minLength(10)
.ensure('email').displayName(this.i18n.tr('userField.Email')).required().email()
.ensure('mobile').displayName(this.i18n.tr('userField.Mobile')).required().then().satisfies((value: any, object: {}) => {
if (!value) return true; // this rule is not wrong, because its purpose is not to check if its required or not
const countryCode = this.swissdataApi.state.country;
let number = new PhoneNumber( value, countryCode.toLowerCase() );
if (!number.isValid()) return false;
if (!number.isPossible()) return false;
if (!number.isMobile()) return false;
return true;
}).withMessage(this.i18n.tr('userField.Invalid mobile number'))
.on(this);
this.validationController.addObject(this);
this.validationController.addRenderer(new AureliaUXFormRenderer());
}
public bind() {
this.instanceChanged();
}
public instanceChanged() {
this._instance = undefined;
if (this.instance === 'state') {
if (this.swissdataApi.state.swissdata.authenticated && this.swissdataApi.state.swissdata.user) {
let instance = new UserModel();
instance.id = this.swissdataApi.state.swissdata.user.id;
instance.updateInstanceFromElement(this.swissdataApi.state.swissdata.user).then(() => {
this._instance = instance;
});
}
} else if (this.instance instanceof UserModel) {
this._instance = this.instance;
}
this.initField();
}
public propertyChanged() {
this.initField();
}
private initField() {
this.processProperty();
}
private processProperty() {
let rightInstance = this._instance instanceof UserModel;
if (!this._instance || !rightInstance) {
log.warn('this._instance [' + this.property + '] is not set properly');
log.debug('this.instance', this.instance);
log.debug('this._instance', this._instance);
return;
}
}
public async updatePassword() {
const result1 = await this.validationController.validate({object: this, propertyName: 'currentPassword'});
const result2 = await this.validationController.validate({object: this, propertyName: 'newPassword'});
if (!result1.valid || !result2.valid) {
return;
}
try {
const body = {
currentPassword: this.currentPassword,
newPassword: this.newPassword
}
await this.swissdataApi.put('/auth/password-change', body).then(jsonify);
this.currentPassword = '';
this.newPassword = '';
notify(this.i18n.tr('userField.Your password has been updated'), {type: 'success'});
this.notify();
} catch (error) {
errorify(error);
}
}
public async requestEmailChange() {
const result = await this.validationController.validate({object: this, propertyName: 'email'});
if (!result.valid) {
return;
}
try {
const body = {
email: this.email
}
const token = await this.swissdataApi.put('/auth/request-email-change', body).then(jsonify);
this.token = token.token;
} catch(error) {
errorify(error);
}
}
public async validateEmailChange() {
const body = {
token: this.token,
code: this.code.trim()
}
try {
const user = await this.swissdataApi.put('/auth/validate-email-change', body).then(jsonify);
this.token = '';
this.code = '';
this.email = '';
if (this._instance) {
this._instance.email = user.email;
this._instance.emailValidated = user.emailValidated;
}
if (this.instance === 'state') this.updateStateUserWithInstance();
notify(this.i18n.tr('userField.Your email has been changed'), {type: 'success'});
this.notify();
} catch (error) {
errorify(error);
}
}
public cancelEmailChange() {
this.token = '';
this.code = '';
this.email = '';
}
public async requestMobileChange() {
const intMobile = new PhoneNumber( this.mobile, this.swissdataApi.state.country.toLowerCase() ).getNumber();
const body = {
mobile: intMobile
}
try {
const token = await this.swissdataApi.put('/auth/request-mobile-change', body).then(jsonify);
this.token = token.token;
} catch (error) {
errorify(error);
}
}
public cancelMobileChange() {
this.token = '';
this.code = '';
this.mobile = '';
}
public async validateMobileChange() {
const body = {
token: this.token,
code: this.code.trim()
};
try {
const user = await this.swissdataApi.put('/auth/validate-mobile-change', body).then(jsonify);
this.token = '';
this.code = '';
this.mobile = '';
if (this._instance) {
this._instance.mobile = user.mobile;
this._instance.mobileValidated = user.mobileValidated;
}
if (this.instance === 'state') this.updateStateUserWithInstance();
notify(this.i18n.tr('userField.Your mobile has been changed'), {type: 'success'});
this.notify();
} catch (error) {
errorify(error);
}
}
public sendEmailCodeAgain() {
this.requestEmailChange();
}
public sendMobileCodeAgain() {
this.requestMobileChange();
}
private updateStateUserWithInstance() {
this.swissdataApi.store.dispatch(updateUser, this._instance, ['email', 'emailValidated', 'mobile', 'mobileValidated']);
}
private notify() {
const customEvent = DOM.createCustomEvent('changed', {bubbles: true});
this.element.dispatchEvent(customEvent);
}
@computedFrom('swissdataApi.state.country')
public get mobileNumberExemple(): string {
const countryCode = this.swissdataApi.state?.country;
if (!countryCode) {
return '';
}
return PhoneNumber.getExample( this.swissdataApi.state.country, 'mobile' ).getNumber( 'international' );
}
}
export function updateUser(state: AppState, user: UserModel, properties: Array<string> | string) {
log.debug('updateUser', state, user, properties);
if (!state.swissdata.user) return state;
const newState = Object.assign({}, state);
if (typeof properties === 'string') properties = [properties];
for (let prop of properties) {
newState.swissdata.user[prop] = user[prop];
}
return newState;
}