Skip to content

Commit

Permalink
Merge pull request #206 from saneyuki/refactor
Browse files Browse the repository at this point in the history
Refactor AccountMap & related codes.
  • Loading branch information
nekoya authored Dec 27, 2016
2 parents 80cd1de + 3f94b94 commit e753e08
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
13 changes: 6 additions & 7 deletions src/agency/account/AgencyAccountGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {AccountGateway, AccountMap} from '../../public/account/AccountGateway';
import {
AccountId,
SponsorData,
SponsorDataList,
dummyAgencyId,
dummySponsorId,
DUMMY_AGENCY_ID,
DUMMY_SPONSOR_ID,
} from '../../public/account/AccountContract';

/**
Expand All @@ -33,9 +32,9 @@ export class AgencyAccountGateway extends AccountGateway {

class SponsorMap extends AccountMap {
protected _dummyAccount: SponsorData = {
id: dummySponsorId,
id: DUMMY_SPONSOR_ID,
exchange_id: 1,
agency_id: dummyAgencyId,
agency_id: DUMMY_AGENCY_ID,
role: 'sponsor',
uuid: '00000000000000000000000000000000',
name: '不明な広告主',
Expand All @@ -51,9 +50,9 @@ async function getSponsor(driver: FetchDriver, sponsorId: AccountId): Promise<Op
return data;
}

async function getSponsorAll(driver: FetchDriver): Promise<Option<SponsorDataList>> {
async function getSponsorAll(driver: FetchDriver): Promise<Option<Array<SponsorData>>> {
const PATH = '/dr/account/sponsors/';
const res = await driver.get(PATH, {});
const data = await handleOkResponseToOption<SponsorDataList>(res);
const data = await handleOkResponseToOption<Array<SponsorData>>(res);
return data;
}
17 changes: 8 additions & 9 deletions src/operator/account/OperatorAccountGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {AgencyAccountGateway} from '../../agency/account/AgencyAccountGateway';
import {
AccountId,
AgencyData,
AgencyDataList,
SponsorDataList,
dummyAgencyId,
DUMMY_AGENCY_ID,
SponsorData,
} from '../../public/account/AccountContract';
import {AccountMap} from '../../public/account/AccountGateway';

Expand All @@ -19,7 +18,7 @@ import {AccountMap} from '../../public/account/AccountGateway';
*/
export class OperatorAccountGateway extends AgencyAccountGateway {

getSponsorAllByAgency(agencyId: AccountId): Promise<Option<SponsorDataList>> {
getSponsorAllByAgency(agencyId: AccountId): Promise<Option<Array<SponsorData>>> {
const result = getSponsorAllByAgency(this._driver, agencyId);
return result;
}
Expand All @@ -40,7 +39,7 @@ export class OperatorAccountGateway extends AgencyAccountGateway {

export class AgencyMap extends AccountMap {
protected _dummyAccount: AgencyData = {
id: dummyAgencyId,
id: DUMMY_AGENCY_ID,
exchange_id: 1,
role: 'agency',
uuid: '00000000000000000000000000000000',
Expand All @@ -49,11 +48,11 @@ export class AgencyMap extends AccountMap {
};
}

async function getSponsorAllByAgency(driver: FetchDriver, agencyId: AccountId): Promise<Option<SponsorDataList>> {
async function getSponsorAllByAgency(driver: FetchDriver, agencyId: AccountId): Promise<Option<Array<SponsorData>>> {
const PATH = '/dr/account/sponsors/';
const url = `${PATH}/?agency_id=${String(agencyId)}`;
const res = await driver.get(url, {});
const data = await handleOkResponseToOption<SponsorDataList>(res);
const data = await handleOkResponseToOption<Array<SponsorData>>(res);
return data;
}

Expand All @@ -65,9 +64,9 @@ async function getAgency(driver: FetchDriver, agencyId: AccountId): Promise<Opti
return data;
}

async function getAgencyAll(driver: FetchDriver): Promise<Option<AgencyDataList>> {
async function getAgencyAll(driver: FetchDriver): Promise<Option<Array<SponsorData>>> {
const PATH = '/dr/account/agencies/';
const res = await driver.get(PATH, {});
const data = await handleOkResponseToOption<AgencyDataList>(res);
const data = await handleOkResponseToOption<Array<SponsorData>>(res);
return data;
}
12 changes: 2 additions & 10 deletions src/public/account/AccountContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,15 @@ export interface AccountData {
readonly name: string; // 社名
readonly person: string; // 担当者名
}
export type AccountDataList = Array<AccountData>;
export type AccountDataMap = Map<AccountId, AccountData>;

export const dummyAgencyId = 3000;
export const dummySponsorId = 3000;
export const DUMMY_AGENCY_ID = 3000;
export const DUMMY_SPONSOR_ID = 3000;

export interface AgencyData extends AccountData {
readonly role: 'agency';
}

export type AgencyDataList = Array<AgencyData>;
export type AgencyDataMap = Map<AccountId, AgencyData>;

export interface SponsorData extends AccountData {
readonly role: 'sponsor';
readonly agency_id: AccountId; // 属するAgencyData.idと一致する
}

export type SponsorDataList = Array<SponsorData>;
export type SponsorDataMap = Map<AccountId, SponsorData>;
24 changes: 12 additions & 12 deletions src/public/account/AccountGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Result} from 'option-t/src/Result';
import {FetchDriver} from '../../lib/FetchDriver';
import {handleOkResponseToResult} from '../../lib/FetchHelper';

import {AccountId, AccountData, AccountDataList, AccountDataMap} from './AccountContract';
import {AccountId, AccountData} from './AccountContract';

export type AccountInfoResult = Result<AccountData, string>;

Expand All @@ -31,19 +31,11 @@ export class AccountGateway {
}

export abstract class AccountMap {
protected _map: AccountDataMap;
protected _map: Map<AccountId, AccountData>;
protected _dummyAccount: AccountData;

constructor(accountList: Option<AccountDataList>) {
this._map = this._generateAccountMap(accountList);
}

_generateAccountMap(accountList: Option<AccountDataList>): AccountDataMap {
const map = new Map();
for (const row of accountList.unwrapOr([])) {
map.set(row.id, row);
}
return map;
constructor(accountList: Option<Array<AccountData>>) {
this._map = generateAccountMap(accountList);
}

get(accountId: AccountId): AccountData {
Expand All @@ -56,6 +48,14 @@ export abstract class AccountMap {
}
}

function generateAccountMap(accountList: Option<Array<AccountData>>): Map<AccountId, AccountData> {
const map = new Map();
for (const row of accountList.unwrapOr([])) {
map.set(row.id, row);
}
return map;
}

export async function getAccountInformation(driver: FetchDriver, param?: GetAccountInfoArgs): Promise<AccountInfoResult> {
const PATH = '/dr/account/me';
let url = PATH;
Expand Down

0 comments on commit e753e08

Please sign in to comment.