Skip to content

Commit

Permalink
feat(the-camp): 인편 보내기 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteKiwi committed Mar 1, 2022
1 parent de956a8 commit 562451c
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/services/the-camp/requesters/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './register-cafe';
export * from './login';
export * from './register-cafe';
export * from './register-soldier';
export * from './send-letter';
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TheCampSession } from '@common/types';
import { Parameter } from '@core/http';
import {
관계,
관계CodeMap,
Expand All @@ -9,7 +10,6 @@ import {
} from '@core/types';
import axios, { AxiosRequestConfig } from 'axios';

import { Parameter } from '../../../../core/http';
import { assertsResponse } from './asserts-response';

export class RegisterCafeRequester {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assertsResponse } from './asserts-response';
import 성공 from './test/성공.json';

describe('assertsResponse', () => {
it('성공', () => {
expect(() => assertsResponse(성공 as any)).not.toThrow();
});

it('실패', () => {
expect(() =>
assertsResponse({
data: {
resultCd: '-1',
},
} as any),
).toThrow('알 수 없는 오류가 발생하였습니다.');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AxiosResponse } from 'axios';

export function assertsResponse(response: AxiosResponse) {
if (response.data.resultCd !== '0000') {
throw new Error(
response.data.resultMsg || '알 수 없는 오류가 발생하였습니다.',
);
}
}
1 change: 1 addition & 0 deletions src/services/the-camp/requesters/send-letter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './send-letter.requester';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { loginRequester } from '../login';
import { sendLetterRequester } from './send-letter.requester';

describe('SendLetterRequester e2e', () => {
it('성공', async () => {
const session = await loginRequester.request({
id: '',
password: '',
});
await sendLetterRequester.request(
{
제목: '내용은 곧 제목',
작성자: '장지훈',
내용: `하이 요즘 뭐해
바쁘니
자니 메롱
냠냠`,
입영부대: '육군훈련소-논산',
입영부대EduId: '',
훈련병Id: '',
},
session,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { TheCampSession } from '@common/types';
import { Parameter } from '@core/http';
import { 입영부대, 입영부대CodeMap } from '@core/types';
import axios, { AxiosRequestConfig } from 'axios';

import { assertsResponse } from './asserts-response';

export class SendLetterRequester {
constructor(private readonly asserts = assertsResponse) {}

async request(dto: SendLetterDto, session: TheCampSession): Promise<void> {
const response = await axios.post(
'https://www.thecamp.or.kr/consolLetter/insertConsolLetterA.do',
this.createPayload(dto),
this.createOptions(session),
);

this.asserts(response);
}

private createPayload({
제목,
작성자,
내용,
입영부대,
입영부대EduId,
훈련병Id,
}: SendLetterDto): string {
return (
new Parameter({
traineeMgrSeq: 훈련병Id,
sympathyLetterSubject: '#제목#제목#제목#제목#',
sympathyLetterContent: this.createLetterContent(작성자, 내용),
trainUnitCd: 입영부대CodeMap[입영부대],
trainUnitEduSeq: 입영부대EduId,
boardDiv: 'sympathyLetter',
tempSaveYn: 'N',
sympathyLetterEditorFileGroupSeq: '',
fileGroupMgrSeq: '',
fileMgrSeq: '',
sympathyLetterMgrSeq: '',
})
.toString()
// 제목은 공백을 +로 치환해야함 일단 하드코딩딩딩
.replace(
encodeURIComponent('#제목#제목#제목#제목#'),
제목.trim().split(' ').map(encodeURIComponent).join('+'),
)
);
}

private createLetterContent(작성자: string, 내용: string): string {
return (
`<p>작성자: ${작성자}</p>` +
내용
.split('\n')
.map((line) => `<p>${line.trim() || '&nbsp;'}</p>`)
.join('')
).replaceAll(' ', '&nbsp;');
}

private createOptions(session: TheCampSession): AxiosRequestConfig {
return {
headers: {
Accept: 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.70 Whale/3.13.131.27 Safari/537.36',
Host: 'www.thecamp.or.kr',
Origin: 'https://www.thecamp.or.kr',
Referer: 'https://www.thecamp.or.kr/eduUnitCafe/viewEduUnitCafeMain.do',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Whale";v="3"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
Cookie: session.cookies
.map(({ key, value }) => `${key}=${value}`)
.join('; '),
},
};
}
}
export interface SendLetterDto {
제목: string;
작성자: string;
내용: string;
입영부대: 입영부대;
// trainUnitEduSeq
입영부대EduId: string;
// traineeMgrSeq
훈련병Id: string;
}

export const sendLetterRequester = new SendLetterRequester();
3 changes: 3 additions & 0 deletions src/services/the-camp/requesters/send-letter/test/성공.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": { "resultCd": "0000", "resultMsg": "정상처리되었습니다." }
}
7 changes: 7 additions & 0 deletions src/services/the-camp/the-camp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
registerCafeRequester as _registerCafeRequester,
RegisterSoldierDto,
registerSoldierRequester as _registerSoldierRequester,
SendLetterDto,
sendLetterRequester as _sendLetterRequester,
} from './requesters';

export class TheCampService {
constructor(
private readonly loginRequester = _loginRequester,
private readonly registerSoldierRequester = _registerSoldierRequester,
private readonly registerCafeRequester = _registerCafeRequester,
private readonly sendLetterRequester = _sendLetterRequester,
) {}

async login(credential: Credential): Promise<TheCampSession> {
Expand All @@ -36,5 +39,9 @@ export class TheCampService {
): Promise<void> {
await this.registerCafeRequester.request(dto, session);
}

async sendLetter(dto: SendLetterDto, session: TheCampSession): Promise<void> {
await this.sendLetterRequester.request(dto, session);
}
}
export const theCampService = new TheCampService();

0 comments on commit 562451c

Please sign in to comment.