Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MYR-696] Add field username #130

Merged
merged 10 commits into from
Sep 1, 2021
4 changes: 2 additions & 2 deletions src/__tests__/acceptance/user.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ describe('UserApplication', function () {
name: 'Abdul Hakim',
bio: 'Hello, my name is Abdul Hakim',
});
await client.patch(`/users/${updatedUser.id}`).send(updatedUser).expect(204);
const result = await userRepository.findById(updatedUser.id);
await client.patch(`/users/${persistedUser.id}`).send(updatedUser).expect(204);
const result = await userRepository.findById(persistedUser.id);
expect(result).to.containEql(updatedUser);
});

Expand Down
4 changes: 1 addition & 3 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export class UserController {
})
user: User,
): Promise<User> {
const newUser = new User(user);
newUser.bio = `Hello, my name is ${newUser.name}!`;
return this.userRepository.create(newUser);
return this.userRepository.create(user);
}

@intercept(PaginationInterceptor.BINDING_KEY)
Expand Down
11 changes: 10 additions & 1 deletion src/interceptors/initial-creation.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ export class InitialCreationInterceptor implements Provider<Interceptor> {
switch (methodName) {
case MethodType.CREATE: {
if (className === ControllerType.USER) {
const newUser = invocationCtx.args[0];
const user = await this.userRepository.findOne({
where: {
id: invocationCtx.args[0].id,
id: newUser.id,
},
});

if (user) throw new HttpErrors.UnprocessableEntity('User already exist!');

newUser.bio = `Hello, my name is ${newUser.name}!`;
newUser.username =
newUser.name.replace(/\s+/g, '').toLowerCase() +
'.' +
Math.random().toString(36).substr(2, 9);

invocationCtx.args[0] = newUser;
break;
}

Expand Down
9 changes: 9 additions & 0 deletions src/models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export class User extends Entity {
})
name: string;

@property({
type: 'string',
default: false,
index: {
unique: true,
},
})
username?: string;

@property({
type: 'string',
})
Expand Down