Skip to content

Commit

Permalink
major: credential을 생성자에서 받고 login을 없애도록 수정 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteKiwi authored Sep 23, 2022
1 parent 25e616e commit a56a3d3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Client lib of the camp

# Installation
```bash
$ y add install the-camp
$ pnpm add the-camp
```

# Usage
Expand All @@ -20,7 +20,7 @@ async function main() {
password: 'password',
});

const soldierIdentifier = await theCampClient.registerSoldier({
const { soldierId } = await theCampClient.registerSoldier({
성분: '예비군인/훈련병',
군종: '육군',
이름: '홍길동',
Expand All @@ -31,7 +31,7 @@ async function main() {
전화번호: '01094862564',
});

await theCampClient.sendLetter(soldierIdentifier, {
await theCampClient.sendLetter(soldierId, {
작성자: '장지훈',
제목: `내용은 곧 제목22`,
내용: `제목은 곧 내용`,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "the-camp",
"version": "1.1.0",
"version": "2.0.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"module": "esm/index.js",
Expand Down
9 changes: 4 additions & 5 deletions src/client/the-camp.client.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { TheCampClient } from './the-camp.client';

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

await theCampClient.login({
const theCampClient = new TheCampClient({
id: process.env.ID!,
password: process.env.PASSWORD!,
});

const soldierIdentifier = await theCampClient.registerSoldier({
const { soldierId } = await theCampClient.registerSoldier({
성분: '예비군인/훈련병',
군종: '육군',
이름: '홍길동',
Expand All @@ -20,7 +19,7 @@ describe('TheCampClient e2e', () => {
전화번호: '01094862564',
});

await theCampClient.sendLetter(soldierIdentifier, {
await theCampClient.sendLetter(soldierId, {
작성자: '장지훈',
제목: `내용은 곧 제목22`,
내용: `제목은 곧 내용`,
Expand Down
71 changes: 45 additions & 26 deletions src/client/the-camp.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,41 @@ import { TheCampService } from '../services/the-camp';

export class TheCampClient {
private readonly theCampService: TheCampService = new TheCampService();
private session!: TheCampSession;

async login(credential: Credential): Promise<void> {
this.session = await this.theCampService.login(credential);
}
constructor(private readonly credential: Credential) {}

async registerSoldier(soldierInfo: SoldierInfo): Promise<SoldierIdentifier> {
if (!this.isLoggedIn()) {
throw new Error('로그인 후에 군인등록이 가능합니다.');
private session?: TheCampSession;
async fetchSession(): Promise<TheCampSession> {
if (!this.session) {
this.session = await this.theCampService.login(this.credential);
}

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

async registerSoldier(
soldierInfo: SoldierInfo,
): Promise<{ soldierId: string }> {
const session = await this.fetchSession();
await this.theCampService.registerSoldier(soldierInfo, session);
const soldiers = await this.theCampService.fetchSoldiers(session);

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

const cafeRegisteredSoldiers = await this.theCampService.fetchSoldiers(
this.session,
session,
);
const soldier = cafeRegisteredSoldiers
.filter((soldier) => soldier.hasCafe)
Expand All @@ -63,32 +72,42 @@ export class TheCampClient {
입영부대Code: soldier.입영부대Code,
입영부대EduId: soldier.입영부대EduId,
},
this.session,
session,
);

return {
const soldierId = this.createSoldierId({
훈련병Id,
입영부대: soldierInfo.입영부대,
입영부대EduId: soldier.입영부대EduId,
};
});
return { soldierId };
}

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

async sendLetter(soldierId: string, letterInfo: LetterInfo): Promise<void> {
const soldierIdentifier = this.parseSoldierId(soldierId);
const session = await this.fetchSession();
await this.theCampService.sendLetter(
{ ...soldierIdentifier, ...letterInfo },
this.session,
session,
);
}

isLoggedIn(): boolean {
return Boolean(this.session);
private createSoldierId({
입영부대,
입영부대EduId,
훈련병Id,
}: SoldierIdentifier): string {
return `${입영부대}-${입영부대EduId}-${훈련병Id}`;
}

private parseSoldierId(soldierId: string): SoldierIdentifier {
const [입영부대, 입영부대EduId, 훈련병Id] = soldierId.split('-');
// TODO: validation
return {
입영부대: 입영부대 as 입영부대,
입영부대EduId,
훈련병Id,
};
}
}

Expand Down

0 comments on commit a56a3d3

Please sign in to comment.