Skip to content

Commit

Permalink
feat(users): verification code context (#146)
Browse files Browse the repository at this point in the history
feat(users): verification code context
  • Loading branch information
a-sava authored Feb 8, 2024
1 parent 120f546 commit 2bf0474
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,14 @@
"out": {
"admin": "allow"
}
},
"context": {
"in": {
"admin": "allow"
},
"out": {
"admin": "allow"
}
}
},
"createdAt": "2022-04-14T05:34:31.363Z"
Expand Down Expand Up @@ -763,6 +771,14 @@
"out": {
"admin": "allow"
}
},
"context": {
"in": {
"admin": "allow"
},
"out": {
"admin": "allow"
}
}
},
"createdAt": "2022-04-14T05:34:31.363Z"
Expand Down Expand Up @@ -826,6 +842,14 @@
"out": {
"admin": "allow"
}
},
"context": {
"in": {
"admin": "allow"
},
"out": {
"admin": "allow"
}
}
},
"createdAt": "2022-04-14T05:34:31.363Z"
Expand Down Expand Up @@ -871,6 +895,14 @@
"out": {
"admin": "allow"
}
},
"context": {
"in": {
"admin": "allow"
},
"out": {
"admin": "allow"
}
}
},
"createdAt": "2022-04-14T05:34:31.363Z"
Expand Down
12 changes: 8 additions & 4 deletions microservices/users/src/methods/confirm-code/send.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Endpoint } from '@lomray/microservice-helpers';
import { IsBoolean, IsEnum, IsString } from 'class-validator';
import { Endpoint, IsUndefinable } from '@lomray/microservice-helpers';
import { IsBoolean, IsEnum, IsObject, IsString } from 'class-validator';
import { getRepository } from 'typeorm';
import ConfirmCode from '@entities/confirm-code';
import { Factory, ConfirmBy } from '@services/confirm/factory';
Expand All @@ -10,6 +10,10 @@ class ConfirmSendInput {

@IsString()
login: string;

@IsObject()
@IsUndefinable()
context?: Record<string, any>;
}

class ConfirmSendOutput {
Expand All @@ -22,8 +26,8 @@ class ConfirmSendOutput {
*/
const send = Endpoint.custom(
() => ({ input: ConfirmSendInput, output: ConfirmSendOutput, description: 'Send confirm code' }),
async ({ type, login }) => {
const service = Factory.create(type, getRepository(ConfirmCode));
async ({ type, login, context }) => {
const service = Factory.create(type, getRepository(ConfirmCode), context);

return {
isSent: await service.send(login),
Expand Down
12 changes: 8 additions & 4 deletions microservices/users/src/methods/user/change-login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Endpoint, IsType } from '@lomray/microservice-helpers';
import { IsBoolean, IsEnum, IsNotEmpty, IsString } from 'class-validator';
import { Endpoint, IsType, IsUndefinable } from '@lomray/microservice-helpers';
import { IsBoolean, IsEnum, IsNotEmpty, IsObject, IsString } from 'class-validator';
import { getCustomRepository, getRepository } from 'typeorm';
import ConfirmCode from '@entities/confirm-code';
import UserRepository from '@repositories/user';
Expand All @@ -21,6 +21,10 @@ class ChangeLoginInput {
@IsType(['string', 'number'])
@IsNotEmpty()
confirmCode: string | number;

@IsObject()
@IsUndefinable()
context?: Record<string, any>;
}

class ChangeLoginOutput {
Expand All @@ -33,8 +37,8 @@ class ChangeLoginOutput {
*/
const changeLogin = Endpoint.custom(
() => ({ input: ChangeLoginInput, output: ChangeLoginOutput, description: 'Change user login' }),
async ({ userId, login, confirmBy, confirmCode }) => {
const confirmService = Factory.create(confirmBy, getRepository(ConfirmCode));
async ({ userId, login, confirmBy, confirmCode, context }) => {
const confirmService = Factory.create(confirmBy, getRepository(ConfirmCode), context);
const service = ChangeLogin.init({
userId,
login,
Expand Down
9 changes: 7 additions & 2 deletions microservices/users/src/methods/user/change-password.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Endpoint, IsType, IsUndefinable } from '@lomray/microservice-helpers';
import { IsBoolean, IsEnum, IsNotEmpty, IsString, ValidateIf } from 'class-validator';
import { IsBoolean, IsEnum, IsNotEmpty, IsObject, IsString, ValidateIf } from 'class-validator';
import { JSONSchema } from 'class-validator-jsonschema';
import { getCustomRepository, getRepository } from 'typeorm';
import ConfirmCode from '@entities/confirm-code';
Expand Down Expand Up @@ -55,6 +55,10 @@ class ChangePasswordInput {
@IsString()
@IsUndefinable()
clearTokensType?: TClearUserTokens;

@IsObject()
@IsUndefinable()
context?: Record<string, any>;
}

class ChangePasswordOutput {
Expand All @@ -79,6 +83,7 @@ const changePassword = Endpoint.custom(
confirmBy,
confirmCode,
allowByAdmin,
context,
clearTokensType,
payload,
}) => {
Expand All @@ -88,7 +93,7 @@ const changePassword = Endpoint.custom(
login,
isConfirmed: (user: User) =>
(confirmBy &&
Factory.create(confirmBy, getRepository(ConfirmCode)).verifyCode(
Factory.create(confirmBy, getRepository(ConfirmCode), context).verifyCode(
user[confirmBy],
confirmCode,
)) ||
Expand Down
10 changes: 7 additions & 3 deletions microservices/users/src/methods/user/sign-up.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Endpoint, IsMeta, IsType } from '@lomray/microservice-helpers';
import { Endpoint, IsMeta, IsType, IsUndefinable } from '@lomray/microservice-helpers';
import { Type } from 'class-transformer';
import { IsEnum, IsNotEmpty, IsObject } from 'class-validator';
import { getCustomRepository, getRepository } from 'typeorm';
Expand All @@ -19,6 +19,10 @@ class SignUpInput {
@IsType(['string', 'number'])
@IsNotEmpty()
confirmCode: string | number;

@IsObject()
@IsUndefinable()
context?: Record<string, any>;
}

class SignUpOutput {
Expand All @@ -33,8 +37,8 @@ class SignUpOutput {
*/
const signUp = Endpoint.custom(
() => ({ input: SignUpInput, output: SignUpOutput, description: 'Sign up user' }),
async ({ fields, confirmBy, confirmCode }) => {
const confirmService = Factory.create(confirmBy, getRepository(ConfirmCode));
async ({ fields, confirmBy, confirmCode, context }) => {
const confirmService = Factory.create(confirmBy, getRepository(ConfirmCode), context);
const service = SignUp.init({
fields,
isConfirmed: () => confirmService.verifyCode(fields[confirmBy], confirmCode),
Expand Down
8 changes: 7 additions & 1 deletion microservices/users/src/services/confirm/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ abstract class Abstract {
*/
protected readonly repository: Repository<ConfirmCode>;

/**
* @protected
*/
protected readonly context?: Record<string, any>;

/**
* @constructor
*/
public constructor(repository: Abstract['repository']) {
public constructor(repository: Abstract['repository'], context?: Abstract['context']) {
this.repository = repository;
this.context = context;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions microservices/users/src/services/confirm/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ class Factory {
/**
* Create confirmation service
*/
static create(type: ConfirmBy, repository: Repository<ConfirmCode>): Abstract {
static create(
type: ConfirmBy,
repository: Repository<ConfirmCode>,
context?: Record<string, any>,
): Abstract {
switch (type) {
case ConfirmBy.email:
return new EmailConfirm(repository);
return new EmailConfirm(repository, context);

case ConfirmBy.phone:
return new PhoneConfirm(repository);
return new PhoneConfirm(repository, context);
}
}
}
Expand Down

0 comments on commit 2bf0474

Please sign in to comment.