Skip to content

Commit

Permalink
[NestJS] Milestone 1: Profile creation
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertoledo committed Apr 25, 2023
1 parent a629622 commit 48601ff
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kyc-nest/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -11,6 +12,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
ProfileModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
23 changes: 23 additions & 0 deletions kyc-nest/src/profile/profile.controller.ts
Original file line number Diff line number Diff line change
@@ -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<Profile> {
return this.profileService.create(profileData);
}

@Get()
async findAll(): Promise<Profile[]> {
return this.profileService.findAll();
}

@Get(':id')
async findById(@Param('id') id: string): Promise<Profile> {
return this.profileService.findById(id);
}
}
45 changes: 45 additions & 0 deletions kyc-nest/src/profile/profile.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions kyc-nest/src/profile/profile.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
38 changes: 38 additions & 0 deletions kyc-nest/src/profile/profile.service.ts
Original file line number Diff line number Diff line change
@@ -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<Profile>,
) {}

async create(profileData: Profile): Promise<Profile> {
const newProfile = this.profileRepository.create({
...profileData,
kycStatus: 'KYCPending',
});
await this.profileRepository.save(newProfile);
return newProfile;
}

async findAll(): Promise<Profile[]> {
return this.profileRepository.find();
}

async findById(id: string): Promise<Profile> {
const options: FindOneOptions<Profile> = {
where: { id },
};

const profile = await this.profileRepository.findOne(options);
if (!profile) {
throw new NotFoundException(`Profile with ID "${id}" not found`);
}
return profile;
}
}

0 comments on commit 48601ff

Please sign in to comment.