-
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.
- Loading branch information
Showing
11 changed files
with
330 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { faker } from '@faker-js/faker'; | ||
|
||
import { | ||
Cache, | ||
CacheConstructorParams, | ||
} from '@khlug/app/domain/cache/model/Cache'; | ||
|
||
function generator(params: Partial<CacheConstructorParams> = {}): Cache { | ||
return new Cache({ | ||
id: params.id ?? faker.string.numeric(3), | ||
name: params.name ?? faker.string.alpha(10), | ||
content: params.content ?? faker.string.alpha(10), | ||
updatedAt: params.updatedAt ?? new Date(), | ||
}); | ||
} | ||
|
||
export const CacheFixture = { | ||
raw: generator, | ||
}; |
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
13 changes: 13 additions & 0 deletions
13
...app/application/infraBlue/command/updateDoorLockPassword/UpdateDoorLockPasswordCommand.ts
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,13 @@ | ||
import { Typeof } from '@khlug/util/types'; | ||
|
||
export class UpdateDoorLockPasswordCommand { | ||
readonly master: string; | ||
readonly forJajudy: string; | ||
readonly forFacilityTeam: string; | ||
|
||
constructor(params: Typeof<UpdateDoorLockPasswordCommand>) { | ||
this.master = params.master; | ||
this.forJajudy = params.forJajudy; | ||
this.forFacilityTeam = params.forFacilityTeam; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...ion/infraBlue/command/updateDoorLockPassword/UpdateDoorLockPasswordCommandHandler.spec.ts
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,91 @@ | ||
import { EntityRepository } from '@mikro-orm/mysql'; | ||
import { getRepositoryToken } from '@mikro-orm/nestjs'; | ||
import { Test } from '@nestjs/testing'; | ||
import { advanceTo, clear } from 'jest-date-mock'; | ||
|
||
import { UpdateDoorLockPasswordCommand } from '@khlug/app/application/infraBlue/command/updateDoorLockPassword/UpdateDoorLockPasswordCommand'; | ||
import { | ||
FACILITY_TEAM_PASSWORD_CACHE_ID, | ||
JAJUDY_PASSWORD_CACHE_ID, | ||
MASTER_PASSWORD_CACHE_ID, | ||
UpdateDoorLockPasswordCommandHandler, | ||
} from '@khlug/app/application/infraBlue/command/updateDoorLockPassword/UpdateDoorLockPasswordCommandHandler'; | ||
|
||
import { Cache } from '@khlug/app/domain/cache/model/Cache'; | ||
|
||
import { CacheFixture } from '@khlug/__test__/fixtures/CacheFixture'; | ||
import { Message } from '@khlug/constant/message'; | ||
|
||
describe('UpdateDoorLockPasswordCommandHandler', () => { | ||
let handler: UpdateDoorLockPasswordCommandHandler; | ||
let cacheRepository: jest.Mocked<EntityRepository<Cache>>; | ||
|
||
beforeEach(async () => { | ||
advanceTo(new Date()); | ||
|
||
const testModule = await Test.createTestingModule({ | ||
providers: [ | ||
UpdateDoorLockPasswordCommandHandler, | ||
{ | ||
provide: getRepositoryToken(Cache), | ||
useValue: { | ||
find: jest.fn(), | ||
getEntityManager: () => ({ persistAndFlush: jest.fn() }), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
handler = testModule.get(UpdateDoorLockPasswordCommandHandler); | ||
cacheRepository = testModule.get(getRepositoryToken(Cache)); | ||
}); | ||
|
||
afterEach(() => clear()); | ||
|
||
describe('execute', () => { | ||
test('비밀번호 정보가 존재하지 않으면 예외를 발생시켜야 한다', async () => { | ||
cacheRepository.find.mockResolvedValue([]); | ||
|
||
const command = new UpdateDoorLockPasswordCommand({ | ||
master: 'master', | ||
forJajudy: 'jajudy', | ||
forFacilityTeam: 'facilityTeam', | ||
}); | ||
await expect(handler.execute(command)).rejects.toThrow( | ||
Message.SOME_DOOR_LOCK_PASSWORD_NOT_FOUND, | ||
); | ||
}); | ||
|
||
test('비밀번호를 변경해야 한다', async () => { | ||
const masterPassword = CacheFixture.raw({ | ||
id: MASTER_PASSWORD_CACHE_ID, | ||
content: 'oldMaster', | ||
}); | ||
const jajudyPassword = CacheFixture.raw({ | ||
id: JAJUDY_PASSWORD_CACHE_ID, | ||
content: 'oldJajudy', | ||
}); | ||
const facilityTeamPassword = CacheFixture.raw({ | ||
id: FACILITY_TEAM_PASSWORD_CACHE_ID, | ||
content: 'oldFacilityTeam', | ||
}); | ||
|
||
cacheRepository.find.mockResolvedValue([ | ||
masterPassword, | ||
jajudyPassword, | ||
facilityTeamPassword, | ||
]); | ||
|
||
const command = new UpdateDoorLockPasswordCommand({ | ||
master: 'newMaster', | ||
forJajudy: 'newJajudy', | ||
forFacilityTeam: 'newFacilityTeam', | ||
}); | ||
await handler.execute(command); | ||
|
||
expect(masterPassword.content).toBe('newMaster'); | ||
expect(jajudyPassword.content).toBe('newJajudy'); | ||
expect(facilityTeamPassword.content).toBe('newFacilityTeam'); | ||
}); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
...lication/infraBlue/command/updateDoorLockPassword/UpdateDoorLockPasswordCommandHandler.ts
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,60 @@ | ||
import { EntityRepository } from '@mikro-orm/mysql'; | ||
import { InjectRepository } from '@mikro-orm/nestjs'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { UpdateDoorLockPasswordCommand } from '@khlug/app/application/infraBlue/command/updateDoorLockPassword/UpdateDoorLockPasswordCommand'; | ||
|
||
import { Cache } from '@khlug/app/domain/cache/model/Cache'; | ||
|
||
import { Message } from '@khlug/constant/message'; | ||
|
||
export const MASTER_PASSWORD_CACHE_ID = '100'; | ||
export const JAJUDY_PASSWORD_CACHE_ID = '101'; | ||
export const FACILITY_TEAM_PASSWORD_CACHE_ID = '102'; | ||
|
||
@CommandHandler(UpdateDoorLockPasswordCommand) | ||
export class UpdateDoorLockPasswordCommandHandler | ||
implements ICommandHandler<UpdateDoorLockPasswordCommand, void> | ||
{ | ||
constructor( | ||
@InjectRepository(Cache) | ||
private readonly cacheRepository: EntityRepository<Cache>, | ||
) {} | ||
|
||
async execute(command: UpdateDoorLockPasswordCommand): Promise<void> { | ||
const { master, forJajudy, forFacilityTeam } = command; | ||
|
||
const passwords = await this.cacheRepository.find({ | ||
id: { | ||
$in: [ | ||
MASTER_PASSWORD_CACHE_ID, | ||
JAJUDY_PASSWORD_CACHE_ID, | ||
FACILITY_TEAM_PASSWORD_CACHE_ID, | ||
], | ||
}, | ||
}); | ||
|
||
const masterPassword = passwords.find( | ||
(password) => password.id === MASTER_PASSWORD_CACHE_ID, | ||
); | ||
const jajudyPassword = passwords.find( | ||
(password) => password.id === JAJUDY_PASSWORD_CACHE_ID, | ||
); | ||
const facilityTeamPassword = passwords.find( | ||
(password) => password.id === FACILITY_TEAM_PASSWORD_CACHE_ID, | ||
); | ||
|
||
if (!masterPassword || !jajudyPassword || !facilityTeamPassword) { | ||
throw new NotFoundException(Message.SOME_DOOR_LOCK_PASSWORD_NOT_FOUND); | ||
} | ||
|
||
masterPassword.updateContent(master); | ||
jajudyPassword.updateContent(forJajudy); | ||
facilityTeamPassword.updateContent(forFacilityTeam); | ||
|
||
await this.cacheRepository | ||
.getEntityManager() | ||
.persistAndFlush([masterPassword, jajudyPassword, facilityTeamPassword]); | ||
} | ||
} |
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
Oops, something went wrong.