-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #605 from boostcampwm2023/BE-SeparateUserService-#599
[BE/#599] userService 관심사 분리
- Loading branch information
Showing
14 changed files
with
321 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { NotificationService } from './notification.service'; | ||
import { RegistrationTokenRepository } from './registrationToken.repository'; | ||
|
||
@Module({ | ||
exports: [NotificationService], | ||
providers: [NotificationService, RegistrationTokenRepository], | ||
}) | ||
export class NotificationModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { NotificationService } from './notification.service'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { RegistrationTokenRepository } from './registrationToken.repository'; | ||
import { RegistrationTokenEntity } from '../entities/registrationToken.entity'; | ||
import { PushMessage } from '../common/fcmHandler'; | ||
|
||
const mockRepository = { | ||
save: jest.fn(), | ||
delete: jest.fn(), | ||
findOne: jest.fn(), | ||
update: jest.fn(), | ||
}; | ||
|
||
const mockAdmin = jest.requireMock('firebase-admin'); | ||
jest.mock('firebase-admin'); | ||
mockAdmin.apps = []; | ||
|
||
describe('NotificationService', () => { | ||
let service: NotificationService; | ||
let repository; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
NotificationService, | ||
{ | ||
provide: RegistrationTokenRepository, | ||
useValue: mockRepository, | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: { get: jest.fn((key: string) => 'mocked-value') }, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<NotificationService>(NotificationService); | ||
repository = module.get(RegistrationTokenRepository); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('getRegistrationToken', function () { | ||
it('should return null when token does not exist', async function () { | ||
repository.findOne.mockResolvedValue(null); | ||
const res = await service.getRegistrationToken('user'); | ||
expect(res).toEqual(null); | ||
}); | ||
|
||
it('should return registration token', async function () { | ||
const registrationToken = new RegistrationTokenEntity(); | ||
registrationToken.registration_token = 'test'; | ||
repository.findOne.mockResolvedValue(registrationToken); | ||
const res = await service.getRegistrationToken('user'); | ||
expect(res).toEqual('test'); | ||
}); | ||
}); | ||
|
||
describe('registerToken', function () { | ||
it('should create token', async function () { | ||
repository.findOne.mockResolvedValue(null); | ||
await service.registerToken('user', 'token'); | ||
expect(repository.save).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should update token', async function () { | ||
repository.findOne.mockResolvedValue(new RegistrationTokenEntity()); | ||
await service.registerToken('user', 'token'); | ||
expect(repository.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('removeRegistrationToken', function () { | ||
it('should remove', async function () { | ||
await service.removeRegistrationToken('userId'); | ||
expect(repository.delete).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('createChatNotificationMessage', function () { | ||
it('should return message', function () { | ||
const pushMessage: PushMessage = { | ||
body: 'message', | ||
data: { | ||
room_id: '123', | ||
}, | ||
title: 'nickname', | ||
}; | ||
const result = service.createChatNotificationMessage( | ||
'token', | ||
pushMessage, | ||
); | ||
expect(result.token).toEqual('token'); | ||
expect(result.notification.title).toEqual('nickname'); | ||
expect(result.notification.body).toEqual('message'); | ||
expect(result.data.room_id).toEqual('123'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import admin from 'firebase-admin'; | ||
import { PushMessage } from '../common/fcmHandler'; | ||
import { RegistrationTokenRepository } from './registrationToken.repository'; | ||
|
||
@Injectable() | ||
export class NotificationService { | ||
private readonly logger = new Logger('ChatsGateway'); | ||
constructor( | ||
private configService: ConfigService, | ||
private registrationTokenRepository: RegistrationTokenRepository, | ||
) { | ||
if (admin.apps.length === 0) { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert( | ||
this.configService.get('GOOGLE_APPLICATION_CREDENTIALS'), | ||
), | ||
}); | ||
this.logger.log('Firebase Admin initialized'); | ||
} | ||
} | ||
|
||
async sendChatNotification(userId: string, pushMessage: PushMessage) { | ||
const registrationToken = await this.getRegistrationToken(userId); | ||
if (!registrationToken) { | ||
throw new Error('no registration token'); | ||
} | ||
const message = this.createChatNotificationMessage( | ||
registrationToken, | ||
pushMessage, | ||
); | ||
try { | ||
const response = await admin.messaging().send(message); | ||
this.logger.debug( | ||
`Push Notification Success : ${response} `, | ||
'FcmHandler', | ||
); | ||
} catch (e) { | ||
throw new Error('fail to send chat notification'); | ||
} | ||
} | ||
|
||
createChatNotificationMessage( | ||
registrationToken: string, | ||
pushMessage: PushMessage, | ||
) { | ||
return { | ||
token: registrationToken, | ||
notification: { | ||
title: pushMessage.title, | ||
body: pushMessage.body, | ||
}, | ||
apns: { | ||
payload: { | ||
aps: { | ||
sound: 'default', | ||
}, | ||
}, | ||
}, | ||
data: { | ||
...pushMessage.data, | ||
}, | ||
}; | ||
} | ||
|
||
async getRegistrationToken(userId: string): Promise<string> { | ||
const registrationToken = | ||
await this.registrationTokenRepository.findOne(userId); | ||
if (registrationToken === null) { | ||
this.logger.error('토큰이 없습니다.', 'FcmHandler'); | ||
return null; | ||
} | ||
return registrationToken.registration_token; | ||
} | ||
|
||
async registerToken(userId, registrationToken) { | ||
const registrationTokenEntity = | ||
await this.registrationTokenRepository.findOne(userId); | ||
if (registrationTokenEntity === null) { | ||
await this.registrationTokenRepository.save(userId, registrationToken); | ||
} else { | ||
await this.registrationTokenRepository.update(userId, registrationToken); | ||
} | ||
} | ||
|
||
async removeRegistrationToken(userId: string) { | ||
await this.registrationTokenRepository.delete(userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Inject, Injectable, Scope } from '@nestjs/common'; | ||
import { BaseRepository } from '../common/base.repository'; | ||
import { DataSource } from 'typeorm'; | ||
import { REQUEST } from '@nestjs/core'; | ||
import { RegistrationTokenEntity } from '../entities/registrationToken.entity'; | ||
|
||
@Injectable({ scope: Scope.REQUEST }) | ||
export class RegistrationTokenRepository extends BaseRepository { | ||
constructor(dataSource: DataSource, @Inject(REQUEST) req: Request) { | ||
super(dataSource, req); | ||
} | ||
async findOne(userId: string): Promise<RegistrationTokenEntity> { | ||
return await this.getRepository(RegistrationTokenEntity).findOne({ | ||
where: { user_hash: userId }, | ||
}); | ||
} | ||
|
||
async save(userId: string, registrationToken: string) { | ||
await this.getRepository(RegistrationTokenEntity).save({ | ||
user_hash: userId, | ||
registration_token: registrationToken, | ||
}); | ||
} | ||
|
||
async update(userId: string, registrationToken: string) { | ||
await this.getRepository(RegistrationTokenEntity).update( | ||
{ | ||
user_hash: userId, | ||
}, | ||
{ registration_token: registrationToken }, | ||
); | ||
} | ||
|
||
async delete(userId: string) { | ||
await this.getRepository(RegistrationTokenEntity).delete({ | ||
user_hash: userId, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Inject, Injectable, Scope } from '@nestjs/common'; | ||
import { BaseRepository } from '../common/base.repository'; | ||
import { DataSource } from 'typeorm'; | ||
import { REQUEST } from '@nestjs/core'; | ||
import { UserEntity } from '../entities/user.entity'; | ||
|
||
@Injectable({ scope: Scope.REQUEST }) | ||
export class UserRepository extends BaseRepository { | ||
constructor(dataSource: DataSource, @Inject(REQUEST) req: Request) { | ||
super(dataSource, req); | ||
} | ||
|
||
async softDeleteCascade(userId: string) { | ||
const user = await this.getRepository(UserEntity).findOne({ | ||
where: { user_hash: userId }, | ||
relations: ['blocker_post', 'blocker'], | ||
}); | ||
await this.getRepository(UserEntity).softRemove(user); | ||
} | ||
} |
Oops, something went wrong.