[FEAT]: create user business logic

This commit is contained in:
behnamrhp 2023-05-21 14:56:24 +03:00
parent 4e18835297
commit cec906f519
18 changed files with 135 additions and 45 deletions

View File

@ -1,8 +1,6 @@
import { CreateAccountDTOReturnType, INewUserDTO } from './protocols'; import { CreateAccountDTOReturnType, INewUserData } from './protocols';
const createAccountDTO = ( const createAccountDTO = (newUser: INewUserData): CreateAccountDTOReturnType => ({
newUser: INewUserDTO
): CreateAccountDTOReturnType => ({
enabled: true, enabled: true,
firstName: newUser.firstname, firstName: newUser.firstname,
lastName: newUser.lastname, lastName: newUser.lastname,

View File

@ -1,4 +1,4 @@
export interface INewUserDTO { export interface INewUserData {
firstname: string; firstname: string;
lastname: string; lastname: string;
phonenumber: string; phonenumber: string;

View File

@ -1,8 +1,8 @@
import { INewUserDTO } from '../dto/protocols'; import { INewUserData } from '../dto/protocols';
import { CreateAccountROReturnType } from '../response-object/protocols'; import { CreateAccountROReturnType } from '../response-object/protocols';
interface ICreateAcountRepo { interface ICreateAcountRepo {
execute: (newUser: INewUserDTO) => Promise<CreateAccountROReturnType>; execute: (newUser: INewUserData) => Promise<CreateAccountROReturnType>;
} }
export default ICreateAcountRepo; export default ICreateAcountRepo;

View File

@ -1,5 +1,5 @@
import createAccountDTO from '../dto/createAccountDTO'; import createAccountDTO from '../dto/createAccountDTO';
import { INewUserDTO } from '../dto/protocols'; import { INewUserData } from '../dto/protocols';
import createAcountRO from '../response-object/createAcountRO'; import createAcountRO from '../response-object/createAcountRO';
import { CreateAccountROReturnType } from '../response-object/protocols'; import { CreateAccountROReturnType } from '../response-object/protocols';
import ICreateAcountRepo from './ICreateAcountRepo'; import ICreateAcountRepo from './ICreateAcountRepo';
@ -12,8 +12,7 @@ export default class CreateAccountRepo implements ICreateAcountRepo {
this.httpHandler = httpHandler; this.httpHandler = httpHandler;
} }
execute: (newUser: INewUserDTO) => Promise<CreateAccountROReturnType> = execute: (newUser: INewUserData) => Promise<CreateAccountROReturnType> = async (newUser: INewUserData) => {
async (newUser: INewUserDTO) => {
// call dto // call dto
const dto = createAccountDTO(newUser); const dto = createAccountDTO(newUser);
// call // call

View File

@ -1,6 +1,4 @@
import { CreateAccountDTOReturnType } from '../dto/protocols'; import { CreateAccountDTOReturnType } from '../dto/protocols';
import { CreateAcountResponseApi } from '../response-object/protocols'; import { CreateAcountResponseApi } from '../response-object/protocols';
export type HttpHandler = ( export type HttpHandler = (newUser: CreateAccountDTOReturnType) => Promise<CreateAcountResponseApi>;
newUser: CreateAccountDTOReturnType
) => Promise<CreateAcountResponseApi>;

View File

@ -1,11 +1,6 @@
import { import { CreateAccountROReturnType, CreateAcountResponseApi } from './protocols';
CreateAccountROReturnType,
CreateAcountResponseApi,
} from './protocols';
const createAcountRO = ( const createAcountRO = (apiResponse: CreateAcountResponseApi): CreateAccountROReturnType => ({
apiResponse: CreateAcountResponseApi
): CreateAccountROReturnType => ({
accountId: apiResponse.id, accountId: apiResponse.id,
phonenumber: apiResponse.username, phonenumber: apiResponse.username,
}); });

View File

@ -0,0 +1,9 @@
import { CreateProfileDtoType, ICreateNewProfileData } from './protocols';
const createProfileDTO: CreateProfileDtoType = (userAccount: ICreateNewProfileData) => ({
account_id: userAccount.accountId,
first_name: userAccount.firstname,
last_name: userAccount.lastname,
});
export default createProfileDTO;

View File

@ -0,0 +1,12 @@
import { INewUserData } from '../../../create-account/data/dto/protocols';
import { CreateAccountROReturnType } from '../../../create-account/data/response-object/protocols';
export type ICreateNewProfileData = CreateAccountROReturnType & INewUserData;
export type CreateProfileDtoReturnType = {
account_id: string;
first_name: string;
last_name: string;
};
export type CreateProfileDtoType = (userAccount: ICreateNewProfileData) => CreateProfileDtoReturnType;

View File

@ -0,0 +1,22 @@
/* eslint-disable no-useless-constructor */
import RepositoryHandler from '~/driven/utils/helpers/repository-handler/repositoryHandler';
import { CreateProfileDtoReturnType, ICreateNewProfileData } from '../dto/protocols';
import createProfileDTO from '../dto/createProfileDTO';
import ICreateProfileRepo from './ICreateProfileRepo';
import { HttpHandler } from './protocols';
export default class CreateProfileRepo
extends RepositoryHandler<CreateProfileDtoReturnType, string>
implements ICreateProfileRepo
{
constructor(httpHandler: HttpHandler) {
super(httpHandler);
}
async execute(accountData: ICreateNewProfileData) {
// create data in dto
const dto = createProfileDTO(accountData);
// call main http handler
return await this.httpHandler(dto);
}
}

View File

@ -0,0 +1,5 @@
import { ICreateNewProfileData } from '../dto/protocols';
export default interface ICreateProfileRepo {
execute: (accountData: ICreateNewProfileData) => Promise<string>;
}

View File

@ -0,0 +1,3 @@
import { CreateProfileDtoReturnType } from '../dto/protocols';
export type HttpHandler = (newUser: CreateProfileDtoReturnType) => Promise<string>;

View File

@ -1,5 +0,0 @@
import UsersModel from '../../../common/data/model/usersModel';
type ICreateUserRepo = () => Promise<UsersModel>;
export default ICreateUserRepo;

View File

@ -1,7 +0,0 @@
import ICreateUserRepo from './ICreateUserRepo';
const creatUserRepo = () => {
};
export default creatUserRepo;

View File

@ -0,0 +1,3 @@
import createUserInfra from './infra/createUserInfra';
export default createUserInfra;

View File

@ -0,0 +1,29 @@
import CreateAccountRepo from '../create-account/data/repository/createAcountRepo';
import { HttpHandler as HttpAccountHandler } from '../create-account/data/repository/protocols';
import CreateProfileRepo from '../create-profile/data/repository/CreateRepositoryRepo';
import { HttpHandler as HttpProfileHandler } from '../create-profile/data/repository/protocols';
import CreateUserUsecase from '../usecase/createUserUsecase';
class createUserInfra {
private httpAccountHandler: HttpAccountHandler;
private httpProfileHandler: HttpProfileHandler;
constructor(httpAccountHandler: HttpAccountHandler, httpProfileHandler: HttpProfileHandler) {
this.httpAccountHandler = httpAccountHandler;
this.httpProfileHandler = httpProfileHandler;
}
execute() {
// make account repositroy ready
const accountRepository = new CreateAccountRepo(this.httpAccountHandler);
// make profile repository ready
const profileRepository = new CreateProfileRepo(this.httpProfileHandler);
// make usecase ready
const usecase = new CreateUserUsecase(accountRepository, profileRepository);
// return prepared method to call and create user
return usecase;
}
}
export default createUserInfra;

View File

@ -0,0 +1,7 @@
import { HttpHandler as HttpProfileHandler } from './create-profile/data/repository/protocols';
import { HttpHandler as HttpAccountHandler } from './create-account/data/repository/protocols';
export default interface createUserPort {
httpAccountHandler: HttpAccountHandler;
httpProfileHandler: HttpProfileHandler;
}

View File

@ -1,14 +1,27 @@
import ICreateUserRepo from '../data/repository/ICreateUserRepo'; import { INewUserData } from '../create-account/data/dto/protocols';
import ICreateAcountRepo from '../create-account/data/repository/ICreateAcountRepo';
import ICreateProfileRepo from '../create-profile/data/repository/ICreateProfileRepo';
export default class CreateUserUsecase { export default class CreateUserUsecase {
private accountRepository: ICreateUserRepo; private accountRepository: ICreateAcountRepo;
constructor(repository: ICreateUserRepo) { private profileRepository: ICreateProfileRepo;
this.repository = repository;
constructor(accountRepository: ICreateAcountRepo, profileRepository: ICreateProfileRepo) {
this.accountRepository = accountRepository;
this.profileRepository = profileRepository;
} }
async execute() { async execute(newUser: INewUserData) {
// create acount // create acount
const newAccountResponse = await this.accountRepository.execute(newUser);
const newProfileData = {
...newAccountResponse,
...newUser,
};
// create profile by account ID // create profile by account ID
const newProfileResponse = await this.profileRepository.execute(newProfileData);
return newProfileResponse;
} }
} }

View File

@ -0,0 +1,9 @@
type HttpHandler<NewDataToAdd, ResponseType> = (newUser: NewDataToAdd) => Promise<ResponseType>;
export default class RepositoryHandler<NewDataToAdd, ResponseType> {
protected httpHandler: HttpHandler<NewDataToAdd, ResponseType>;
constructor(httpHandler: HttpHandler<NewDataToAdd, ResponseType>) {
this.httpHandler = httpHandler;
}
}