Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --verbose",
"test": "jest --verbose --detectOpenHandles --forceExit",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand All @@ -33,6 +33,8 @@
"@nestjs/typeorm": "^10.0.2",
"@nestjs/websockets": "^10.3.10",
"@ssut/nestjs-sqs": "^2.2.0",
"@testcontainers/postgresql": "^10.16.0",
"@testcontainers/redis": "^10.16.0",
"@types/nodemailer": "^6.4.16",
"@types/passport-jwt": "^4.0.1",
"@types/socket.io": "^3.0.2",
Expand All @@ -49,6 +51,7 @@
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"pg": "^8.11.5",
"pg-mem": "^3.0.4",
"prom-client": "^15.1.3",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
Expand All @@ -57,6 +60,7 @@
"speakeasy": "^2.0.0",
"tsid-ts": "^0.0.9",
"typeorm": "^0.3.20",
"typeorm-extension": "^3.6.3",
"typeorm-naming-strategies": "^4.1.0"
},
"devDependencies": {
Expand All @@ -76,7 +80,7 @@
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"supertest": "^7.0.0",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
Expand All @@ -89,6 +93,12 @@
"json",
"ts"
],
"setupFiles": [
"<rootDir>/test/setup/jest.setup.ts"
],
"setupFilesAfterEnv": [
"<rootDir>/test/setup/jest.setup.ts"
],
"roots": [
"<rootDir>/src",
"<rootDir>/test"
Expand Down
14 changes: 13 additions & 1 deletion src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';

describe('AppController', () => {
let appController: AppController;
let app: INestApplication;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
const moduleFutures = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();

app = moduleFutures.createNestApplication();
appController = app.get<AppController>(AppController);
await app.init();
});

test('integration test ', async () => {
await request(app.getHttpServer())
.get('/auth')
.expect(200)
.expect('Hello World!');
});

describe('root', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/auth/application/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('AuthService', () => {
.spyOn(jwtService, 'sign')
.mockReturnValueOnce('test-access-token')
.mockReturnValueOnce('test-refresh-token');
jest.spyOn(cacheService, 'set').mockResolvedValueOnce(undefined);
jest.spyOn(cacheService, 'set').mockResolvedValueOnce('OK');
jest.spyOn(userService, 'update').mockResolvedValueOnce(userDto);

const result = await service.login(userDto);
Expand Down
30 changes: 7 additions & 23 deletions src/auth/application/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import { Inject, Injectable, Logger } from '@nestjs/common';
import { JwtService, JwtSignOptions } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { CACHE_SERVICE, ICacheService } from '../../common/cache/cache.service';
import {
BadRequestException,
UnauthorizedException,
} from '@nestjs/common/exceptions';
import { BadRequestException, UnauthorizedException } from '@nestjs/common/exceptions';
import { UserService } from './user.service';
import { UserDto } from '../presentation/user.dto';
import { AuthDto } from '../presentation/auth.dto';
import { Builder } from 'builder-pattern';
import { ISecurityService, SECURITY_SERVICE } from './security.service';
import { Sender } from '../../common/sender/sender.interface';
import {
ISmsService,
SMS_SERVICE,
} from '../../common/sender/sms/application/sms.service';
import { ISmsService, SMS_SERVICE } from '../../common/sender/sms/application/sms.service';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -62,7 +56,7 @@ export class AuthService {
return user ?? (await this.userService.create(dto));
});

user.userId = user.customerId ?? user.driverId ?? user.businessId;
user.userId = user.userId ?? user.customerId ?? user.driverId ?? user.businessId;

user.userType = dto.userType;

Expand Down Expand Up @@ -101,7 +95,7 @@ export class AuthService {

return Builder(AuthDto)
.uuid(user.uuid)
.name(dto.name)
.name(user.name ?? dto.name)
.userId(user.userId)
.userType(user.userType)
.phoneNumber(user.phoneNumber)
Expand Down Expand Up @@ -175,31 +169,21 @@ export class AuthService {
if (existingToken) await this.cacheService.del(existingToken);
}

await this.cacheService.set(
key,
accessToken,
const expiresIn = Math.floor(
(this.accessTokenOption.expiresIn as number) / 1000,
);

await this.cacheService.set(
accessToken,
JSON.stringify({
...user,
refreshToken: undefined,
}),
(this.accessTokenOption.expiresIn as number) / 1000,
);
await this.cacheService.set(key, accessToken, expiresIn);

await this.cacheService.set(
accessToken,
JSON.stringify({
...user,
refreshToken: undefined,
}),
(this.accessTokenOption.expiresIn as number) / 1000,
expiresIn,
);
}

async getUser(token: string): Promise<any> {
const payload = await this.jwtService.verify(token);
if (!payload) {
Expand Down
9 changes: 3 additions & 6 deletions src/auth/application/jwt-refresh.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { ConfigService } from '@nestjs/config';
import {
BadRequestException,
UnauthorizedException,
} from '@nestjs/common/exceptions';
import { BadRequestException, UnauthorizedException } from '@nestjs/common/exceptions';
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { CustomerService } from 'src/customer/application/customer.service';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { CustomerService } from '../../customer/application/customer.service';
import { BusinessService } from '../../business/application/business.service';
import { DriverService } from '../../driver/application/driver.service';
import { IUserService } from '../user.interface';
Expand Down
2 changes: 1 addition & 1 deletion src/auth/application/security.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as crypto from 'crypto';
import * as speakeasy from 'speakeasy';
import { ConfigService } from '@nestjs/config';

export const SECURITY_SERVICE = 'SECURITY_SERVICE';
export const SECURITY_SERVICE = Symbol('SECURITY_SERVICE');

export interface ISecurityService {
encrypt(text: string): string | undefined;
Expand Down
5 changes: 3 additions & 2 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { AuthService } from './application/auth.service';
import { AuthController } from './presentation/auth.controller';
import { CustomerModule } from 'src/customer/customer.module';
import { CustomerModule } from '../customer/customer.module';
import { JwtModule } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtAccessStrategy } from './application/jwt-access.strategy';
import { JwtRefreshStrategy } from './application/jwt-refresh.strategy';
import { PassportModule } from '@nestjs/passport';
Expand All @@ -17,6 +17,7 @@ import { SmsModule } from '../common/sender/sms/sms.module';
@Global()
@Module({
imports: [
ConfigModule,
JwtModule.registerAsync({
useFactory: async (configService: ConfigService) => ({
secret: configService.get<string>('jwt/access/secret'),
Expand Down
7 changes: 7 additions & 0 deletions src/auth/presentation/auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ export class OtpResponseDto implements OtpResponse {
readOnly: true,
})
verified: boolean;

@ApiProperty({
description: '์ „์†ก ๋งค์ฒด',
type: String,
readOnly: true,
})
sendType: string;
}
6 changes: 4 additions & 2 deletions src/auth/presentation/user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ export class UserDto {

static from(customer: Customer): UserDto {
return Builder(UserDto)
.userId(customer.customerId)
.uuid(customer.uuid)
.name(customer.customerName)
.userId(customer.customerId)
.customerId(customer.customerId)
.userType('customer')
.phoneNumber(customer.customerPhoneNumber)
.authProvider(customer.authProvider)
.uuid(customer.uuid)
.build();
}
}
22 changes: 12 additions & 10 deletions src/common/broker/consumer/consumer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ import { SqsOptions } from '@ssut/nestjs-sqs/dist/sqs.types';
});

return {
consumers: [
{
name: 's3-image-object-created',
queueUrl: <string>(
configService.get(`sqs/url/${sqsName.s3ImageCreated}`)
),
region: <string>configService.get('AWS_REGION'),
sqs: sqsClient,
},
],
consumers: configService.get(`sqs/url/${sqsName.s3ImageCreated}`)
? [
{
name: 's3-image-object-created',
queueUrl: <string>(
configService.get(`sqs/url/${sqsName.s3ImageCreated}`)
),
region: <string>configService.get('AWS_REGION'),
sqs: sqsClient,
},
]
: [],
};
},
}),
Expand Down
13 changes: 13 additions & 0 deletions src/common/entity/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Column } from 'typeorm';
import { HasUuid } from './parent.entity';
import { AuthProvider } from '../../auth/presentation/user.dto';

export abstract class UserEntity extends HasUuid {
@Column({
type: 'enum',
enum: AuthProvider,
enumName: 'auth_provider',
nullable: false,
})
authProvider: AuthProvider;
}
7 changes: 2 additions & 5 deletions src/common/filters/exception.filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import {
HttpException,
HttpStatus,
Logger,
NotFoundException,
NotFoundException
} from '@nestjs/common';
import { ResponseEntity } from '../dto/response.entity';
import { Response } from 'express';
import {
BadRequestException,
UnauthorizedException,
} from '@nestjs/common/exceptions';
import { BadRequestException, UnauthorizedException } from '@nestjs/common/exceptions';
import { EntityNotFoundError } from 'typeorm';

@Catch()
Expand Down
3 changes: 2 additions & 1 deletion src/common/image/application/image.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ export class ImageConsumer {

@SqsConsumerEventHandler('s3-image-object-created', 'error')
public async errorHandler(error: Error, message: Message): Promise<void> {
this.logger.error(`Error: ${error.message}`)
this.logger.error(
`Error occurred while processing message: ${message.MessageId}`,
`Error occurred while processing message: ${message?.MessageId}`,
);
}

Expand Down
19 changes: 6 additions & 13 deletions src/customer/application/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ import { CustomerDto } from '../presentation/customer.dto';
import { IUserService } from '../../auth/user.interface';
import { AuthDto } from '../../auth/presentation/auth.dto';
import { UserDto, UserType } from '../../auth/presentation/user.dto';
import {
ISecurityService,
SECURITY_SERVICE,
} from '../../auth/application/security.service';
import { ISecurityService, SECURITY_SERVICE } from '../../auth/application/security.service';
import { ImageService } from '../../common/image/application/image.service';
import { BadRequestException } from '@nestjs/common/exceptions';
import { Customer, ICustomer } from '../customer.domain';
import {
CUSTOMER_REPOSITORY,
ICustomerRepository,
} from '../port/customer.repository';
import { CUSTOMER_REPOSITORY, ICustomerRepository } from '../port/customer.repository';
import { IUUIDHolder, UUID_HOLDER } from '../../common/holder/uuid.holders';
import { DATE_HOLDER, IDateHolder } from '../../common/holder/date.holder';

Expand Down Expand Up @@ -50,14 +44,13 @@ export class CustomerService implements IUserService {
}

return await this.customerRepository.save(
this.customerRepository.create(
Customer.from(dto, this.uuidHolder, this.dateHolder),
),
Customer.create(dto, this.uuidHolder, this.dateHolder),
);
}

findOne(dto: Partial<AuthDto>): Promise<UserDto | null> {
return this.customerRepository.findOne(dto);
async findOne(dto: Partial<AuthDto>): Promise<UserDto | null> {
const customer = await this.customerRepository.findOne(dto);
return customer ? UserDto.from(customer) : null;
}

async getOne(
Expand Down
7 changes: 4 additions & 3 deletions src/customer/customer.domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IUUIDHolder } from '../common/holder/uuid.holders';
import { IDateHolder } from '../common/holder/date.holder';
import { PresignedUrlDto } from '../common/cloud/aws/s3/presentation/presigned-url.dto';
import { ImageDto } from '../common/image/presentation/image.dto';
import { BadRequestException } from '@nestjs/common/exceptions';

export interface ICustomer extends AuthDto {
customerId?: number;
Expand Down Expand Up @@ -53,17 +54,17 @@ export class Customer implements ICustomer, UserDto {
presignedUrlDto?: PresignedUrlDto;
profileImage?: ImageDto;

static from(
static create(
customer: CustomerDto,
uuidHolder: IUUIDHolder,
dateHolder: IDateHolder,
): Customer {
if ((!customer.customerName && !customer.name) || !customer.authProvider) {
throw new Error('ํ•„์ˆ˜ ์ •๋ณด๊ฐ€ ๋ˆ„๋ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
throw new BadRequestException('ํ•„์ˆ˜ ์ •๋ณด๊ฐ€ ๋ˆ„๋ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
}

return Builder<Customer>()
.uuid(uuidHolder.generatedUuid())
.uuid(customer.uuid ?? uuidHolder.generatedUuid())
.customerName(customer.customerName ?? customer.name)
.authProvider(customer.authProvider)
.createdAt(dateHolder.now())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { PreRegistrationSurveyRequest } from './pre-registration-survey-request';
import { EmailService } from 'src/email/email.service';
import { EmailService } from '../../email/email.service';
import { EmailContentGenerator } from './email-content-generator';

@Injectable()
Expand Down
Loading