From 48601ff550dc2e85d44cbe7c7db408205741ecb9 Mon Sep 17 00:00:00 2001 From: Javier Toledo Date: Thu, 30 Mar 2023 11:48:39 +0100 Subject: [PATCH] [NestJS] Milestone 1: Profile creation --- kyc-nest/src/app.module.ts | 2 + kyc-nest/src/profile/profile.controller.ts | 23 +++++++++++ kyc-nest/src/profile/profile.entity.ts | 45 ++++++++++++++++++++++ kyc-nest/src/profile/profile.module.ts | 12 ++++++ kyc-nest/src/profile/profile.service.ts | 38 ++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 kyc-nest/src/profile/profile.controller.ts create mode 100644 kyc-nest/src/profile/profile.entity.ts create mode 100644 kyc-nest/src/profile/profile.module.ts create mode 100644 kyc-nest/src/profile/profile.service.ts diff --git a/kyc-nest/src/app.module.ts b/kyc-nest/src/app.module.ts index 51a6829..c3dc8a1 100644 --- a/kyc-nest/src/app.module.ts +++ b/kyc-nest/src/app.module.ts @@ -1,6 +1,7 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; +import { ProfileModule } from './profile/profile.module'; import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ @@ -11,6 +12,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; entities: [__dirname + '/**/*.entity{.ts,.js}'], synchronize: true, }), + ProfileModule, ], controllers: [AppController], providers: [AppService], diff --git a/kyc-nest/src/profile/profile.controller.ts b/kyc-nest/src/profile/profile.controller.ts new file mode 100644 index 0000000..e3ec23c --- /dev/null +++ b/kyc-nest/src/profile/profile.controller.ts @@ -0,0 +1,23 @@ +import { Controller, Get, Param, Post, Body } from '@nestjs/common'; +import { ProfileService } from './profile.service'; +import { Profile } from './profile.entity'; + +@Controller('profiles') +export class ProfileController { + constructor(private readonly profileService: ProfileService) {} + + @Post() + async create(@Body() profileData: Profile): Promise { + return this.profileService.create(profileData); + } + + @Get() + async findAll(): Promise { + return this.profileService.findAll(); + } + + @Get(':id') + async findById(@Param('id') id: string): Promise { + return this.profileService.findById(id); + } +} diff --git a/kyc-nest/src/profile/profile.entity.ts b/kyc-nest/src/profile/profile.entity.ts new file mode 100644 index 0000000..d98c26a --- /dev/null +++ b/kyc-nest/src/profile/profile.entity.ts @@ -0,0 +1,45 @@ +import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; + +export type KYCStatus = 'KYCPending'; + +@Entity() +export class Profile { + @PrimaryGeneratedColumn() + id: string; + + @Column() + firstName: string; + + @Column() + lastName: string; + + @Column() + address: string; + + @Column() + city: string; + + @Column() + state: string; + + @Column() + zipCode: string; + + @Column() + dateOfBirth: Date; + + @Column() + phoneNumber: string; + + @Column() + email: string; + + @Column({ nullable: true }) + ssn?: string; + + @Column({ nullable: true }) + tin?: string; + + @Column({ default: 'KYCPending' }) + kycStatus: KYCStatus; +} diff --git a/kyc-nest/src/profile/profile.module.ts b/kyc-nest/src/profile/profile.module.ts new file mode 100644 index 0000000..3104c25 --- /dev/null +++ b/kyc-nest/src/profile/profile.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { ProfileController } from './profile.controller'; +import { ProfileService } from './profile.service'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Profile } from './profile.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([Profile])], + controllers: [ProfileController], + providers: [ProfileService], +}) +export class ProfileModule {} diff --git a/kyc-nest/src/profile/profile.service.ts b/kyc-nest/src/profile/profile.service.ts new file mode 100644 index 0000000..95c4573 --- /dev/null +++ b/kyc-nest/src/profile/profile.service.ts @@ -0,0 +1,38 @@ +import { BadRequestException, Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { FindOneOptions, Repository } from 'typeorm'; +import { KYCStatus, Profile } from './profile.entity'; +import { NotFoundException } from '@nestjs/common'; + +@Injectable() +export class ProfileService { + constructor( + @InjectRepository(Profile) + private readonly profileRepository: Repository, + ) {} + + async create(profileData: Profile): Promise { + const newProfile = this.profileRepository.create({ + ...profileData, + kycStatus: 'KYCPending', + }); + await this.profileRepository.save(newProfile); + return newProfile; + } + + async findAll(): Promise { + return this.profileRepository.find(); + } + + async findById(id: string): Promise { + const options: FindOneOptions = { + where: { id }, + }; + + const profile = await this.profileRepository.findOne(options); + if (!profile) { + throw new NotFoundException(`Profile with ID "${id}" not found`); + } + return profile; + } +}