Skip to content

Commit

Permalink
feat(client): registerSoldier 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteKiwi committed Mar 5, 2022
1 parent c54f0b2 commit f445c65
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 70 deletions.
44 changes: 18 additions & 26 deletions src/client/the-camp.client.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
import { TheCampClient } from './the-camp.client';

describe.skip('TheCampClient e2e', () => {
describe('TheCampClient e2e', () => {
it('성공', async () => {
const theCampClient = new TheCampClient();

await theCampClient.login({
id: '',
password: '',
id: process.env.ID!,
password: process.env.PASSWORD!,
});

await theCampClient.sendLetter(
{
성분: '예비군인/훈련병',
군종: '육군',
이름: '홍길동',
입영부대: '육군훈련소-논산',
관계: '',
생년월일: '2001-01-01',
입영일: '2022-02-14',
전화번호: '01012341234',
const soldierIdentifier = await theCampClient.registerSoldier({
성분: '예비군인/훈련병',
군종: '육군',
이름: '홍길동',
입영부대: '육군훈련소-논산',
관계: '',
생년월일: '2001-11-26',
입영일: '2022-02-14',
전화번호: '01094862564',
});

생년월일Code: '08IyuIy6/tXS/vveGiNc+Q==',
입영부대TypeCode: '0000140001',
입영부대EduId: '',
훈련병Id: '',
},
{
작성자: '장지훈',
제목: `내용은 곧 제목`,
내용: `제목은
내용`,
},
);
await theCampClient.sendLetter(soldierIdentifier, {
작성자: '장지훈',
제목: `내용은 곧 제목22`,
내용: `제목은 곧 내용`,
});
});
});
80 changes: 68 additions & 12 deletions src/client/the-camp.client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { TheCampSession } from '@common/types';
import { Credential, 관계, 군종, 성분, 입영부대 } from '@core/types';
import {
Credential,
관계,
군종,
성분,
입영부대,
입영부대CodeMap,
} from '@core/types';

import { TheCampService } from '../services/the-camp';

Expand All @@ -11,20 +18,71 @@ export class TheCampClient {
this.session = await this.theCampService.login(credential);
}

async registerSoldier(soldierInfo: SoldierInfo): Promise<SoldierIdentifier> {
if (!this.isLoggedIn()) {
throw new Error('로그인 후에 군인등록이 가능합니다.');
}

await this.theCampService.registerSoldier(soldierInfo, this.session);
const soldiers = await this.theCampService.fetchSoldiers(this.session);

for (const soldier of soldiers) {
// 카페 가입되어 있으면 스킵
if (soldier.hasCafe) continue;
// 가입 안되어있으면 가입시켜~~
await this.theCampService.registerCafe(
{ ...soldierInfo, 입영부대TypeCode: soldier.입영부대TypeCode },
this.session,
);
}

const cafeRegisteredSoldiers = await this.theCampService.fetchSoldiers(
this.session,
);
const soldier = cafeRegisteredSoldiers
.filter((soldier) => soldier.hasCafe)
.find((soldier) => {
if (soldier.이름 !== soldierInfo.이름) {
return false;
}
if (soldier.입영부대Code !== 입영부대CodeMap[soldierInfo.입영부대]) {
return false;
}
if (soldier.입영일 !== soldierInfo.입영일) {
return false;
}
return true;
});

if (!soldier || !soldier.hasCafe) {
throw new Error('군인의 카페가 개설되지 않았습니다.');
}

const [{ 훈련병Id }] = await this.theCampService.fetchUnitSoldiers(
{
입영부대Code: soldier.입영부대Code,
입영부대EduId: soldier.입영부대EduId,
},
this.session,
);

return {
훈련병Id,
입영부대: soldierInfo.입영부대,
입영부대EduId: soldier.입영부대EduId,
};
}

async sendLetter(
soldierInfo: SoldierInfo,
soldierIdentifier: SoldierIdentifier,
letterInfo: LetterInfo,
): Promise<void> {
if (!this.isLoggedIn()) {
throw new Error('로그인 후에 위문편지 전송이 가능합니다.');
}

// TODO: soldierInfo를 1회만 만들어서 재사용 할 수 있는 방법 생각하여 registerSoldier, registerCafe를 sendLetter에서 분리하기
await this.theCampService.registerSoldier(soldierInfo, this.session);
await this.theCampService.registerCafe(soldierInfo, this.session);

await this.theCampService.sendLetter(
{ ...soldierInfo, ...letterInfo },
{ ...soldierIdentifier, ...letterInfo },
this.session,
);
}
Expand All @@ -44,15 +102,13 @@ export interface SoldierInfo {
생년월일: string; // yyyy-MM-dd
입영일: string; // yyyy-MM-dd
전화번호?: string;
}

// TODO: 아래 필드들 사이트에서 fetch 하도록 작업
생년월일Code: string;
입영부대TypeCode: string;

export interface SoldierIdentifier {
입영부대: 입영부대;
입영부대EduId: string;
훈련병Id: string;
}

export interface LetterInfo {
제목: string;
내용: string;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './client';
export * from './core/types';
export * from './services/the-camp';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class FetchSoldiersRequester {

async request(session: TheCampSession): Promise<FetchSoldierRawInfo[]> {
const response = await axios.get(
'https://www.thecamp.or.kr/eduUnitCafe/viewEduUnitCafeMain.do',
'https://www.thecamp.or.kr/main/viewWebMain.do',
this.createOptions(session),
);

Expand Down
140 changes: 128 additions & 12 deletions src/services/the-camp/requesters/fetch-soldiers/parse-soldiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,169 @@ describe('parseSoldiers', () => {

expect(parseSoldiers({ data } as any)).toEqual([
{
hasCafe: true,
입영부대Code: '20020191700',
입영부대EduId: '6506',
군인Code: '6142000',
입영일: '20210302',
생년월일: '20010928',
입영일: '2021-03-02',
생년월일yyyyMMdd_CanBeEncoded: '20010928',
이름: '김민석',
},
{
hasCafe: true,
입영부대Code: '20121190200',
입영부대EduId: '6611',
군인Code: '3051000',
입영일: '20210329',
생년월일: '20010822',
입영일: '2021-03-29',
생년월일yyyyMMdd_CanBeEncoded: '20010822',
이름: '이예건',
},
{
hasCafe: true,
입영부대Code: '20220280600',
입영부대EduId: '6629',
군인Code: '2050000',
입영일: '20210406',
생년월일: '20010814',
입영일: '2021-04-06',
생년월일yyyyMMdd_CanBeEncoded: '20010814',
이름: '이지원',
},
{
hasCafe: true,
입영부대Code: '20020191700',
입영부대EduId: '6698',
군인Code: '6142000',
입영일: '20210426',
생년월일: '20010405',
입영일: '2021-04-26',
생년월일yyyyMMdd_CanBeEncoded: '20010405',
이름: '김민수',
},
{
hasCafe: true,
입영부대Code: '20220280100',
입영부대EduId: '12117',
군인Code: '2031000',
입영일: '20220125',
생년월일: '20011020',
입영일: '2022-01-25',
생년월일yyyyMMdd_CanBeEncoded: '20011020',
이름: '김명훈',
},
{
hasCafe: true,
입영부대Code: '20020191700',
입영부대EduId: '14030',
군인Code: '6142000',
입영일: '20220214',
생년월일: '20011126',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
이름: '이상택',
},
{
hasCafe: false,
정렬번호: '9',
이름: '이상택',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '08IyuIy6/tXS/vveGiNc+Q==',
입영부대TypeCode: '',
입영부대Code: '',
군종Code: '0000010001',
관계Code: '0000420006',
},
{
hasCafe: false,
정렬번호: '10',
이름: '이상태',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '08IyuIy6/tXS/vveGiNc+Q==',
입영부대TypeCode: '',
입영부대Code: '',
군종Code: '0000010001',
관계Code: '0000420006',
},
{
hasCafe: false,
정렬번호: '11',
이름: '이상택',
입영일: '2022-02-13',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '0000140001',
입영부대Code: '20020191700',
군종Code: '0000010001',
관계Code: '0000420007',
},
{
hasCafe: false,
정렬번호: '12',
이름: '이상택',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '',
입영부대Code: 'undefined',
군종Code: '0000010001',
관계Code: '0000420007',
},
{
hasCafe: false,
정렬번호: '13',
이름: '이상가',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '0000140001',
입영부대Code: '20020191700',
군종Code: '0000010001',
관계Code: '0000420007',
},
{
hasCafe: false,
정렬번호: '14',
이름: '이상나',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '0000140001',
입영부대Code: '20020191700',
군종Code: '0000010001',
관계Code: '0000420007',
},
{
hasCafe: false,
정렬번호: '15',
이름: '이상다',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '0000140001',
입영부대Code: '20020191700',
군종Code: '0000010001',
관계Code: '0000420007',
},
{
hasCafe: false,
정렬번호: '16',
이름: '이상라',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '0000140001',
입영부대Code: '20020191700',
군종Code: '0000010001',
관계Code: '0000420007',
},
{
hasCafe: false,
정렬번호: '17',
이름: '이상마',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '0000140001',
입영부대Code: '20020191700',
군종Code: '0000010001',
관계Code: '0000420007',
},
{
hasCafe: false,
정렬번호: '18',
이름: '이상바',
입영일: '2022-02-14',
생년월일yyyyMMdd_CanBeEncoded: '20011126',
입영부대TypeCode: '0000140001',
입영부대Code: '20020191700',
군종Code: '0000010001',
관계Code: '0000420007',
},
]);
});
});
Loading

0 comments on commit f445c65

Please sign in to comment.