Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/repositories/ItemRateRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import DBService, {
DBLevel,
DefaultOrDateItemKey,
FacilityIndexKey,
FacilityItemValues,
FacilityValues,
FormattedDate,
LevelDefaultTyping
} from '../services/DBService';
import ApiError from '../exceptions/ApiError';
import { AbstractSublevel } from 'abstract-level';
import { Rates } from '../proto/lpms';


abstract class ItemRateRepository {
protected dbService = DBService.getInstance();
protected db: AbstractSublevel<AbstractSublevel<AbstractSublevel<DBLevel, LevelDefaultTyping, string, FacilityValues>,
LevelDefaultTyping,
string,
FacilityItemValues>,
LevelDefaultTyping,
DefaultOrDateItemKey,
Rates>;

protected constructor(
facilityId: string,
indexKey: FacilityIndexKey,
itemId: string
) {
this.db = this.dbService.getItemRatesDB(facilityId, indexKey, itemId);
}

public async getRate(key: DefaultOrDateItemKey): Promise<Rates | null> {
try {
return await this.db.get(key);
} catch (e) {
if (e.status === 404) {
throw ApiError.NotFound(`Unable to get "${key}" of rate level"`);
}
throw e;
}
}

public async setRate(
key: FormattedDate,
value: Rates
): Promise<void> {
await this.db.put(key, value);
}

public async setRateDefault(value: Rates): Promise<void> {
await this.db.put('default', value);
}

public async delRate(key: DefaultOrDateItemKey): Promise<void> {
await this.db.del(key);
}
}

export class SpaceRateRepository extends ItemRateRepository {
constructor(facilityId: string, itemId: string) {
super(facilityId, 'spaces', itemId);
}
}

export class OtherItemRateRepository extends ItemRateRepository {
constructor(facilityId: string, itemId: string) {
super(facilityId, 'otherItems', itemId);
}
}
20 changes: 12 additions & 8 deletions src/services/DBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,6 @@ export default class DBService {
>('availability', { valueEncoding: 'json' });
}

public getSpaceRatesDB(facilityId: string, spaceId: string) {
return this.getFacilityItemDB(facilityId, 'spaces', spaceId).sublevel<
DefaultOrDateItemKey,
Rates
>('rates', { valueEncoding: 'json' });
}

public getSpaceStubsDB(facilityId: string, spaceId: string) {
return this.getFacilityItemDB(facilityId, 'spaces', spaceId).sublevel<
SpaceStubKey,
Expand All @@ -195,6 +188,17 @@ export default class DBService {
return this.getFacilityItemDB(facilityId, indexKey, itemId).sublevel<
RulesItemKey,
Rules
>('rules', { valueEncoding: 'json' });
>('rules', { valueEncoding: 'json' });
}

public getItemRatesDB(
facilityId: string,
indexKey: FacilityIndexKey,
spaceId: string
) {
return this.getFacilityItemDB(facilityId, indexKey, spaceId).sublevel<
DefaultOrDateItemKey,
Rates
>('rates', { valueEncoding: 'json' });
}
}
47 changes: 43 additions & 4 deletions test/repository.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FacilityRuleRepository, SpaceRuleRepository } from '../src/repositories/RuleRepository';
import { NoticeRequiredRule } from '../src/proto/lpms';
import { NoticeRequiredRule, Rates } from '../src/proto/lpms';
import { expect } from 'chai';
import { SpaceRateRepository } from '../src/repositories/ItemRateRepository';

describe('facility repository test', async () => {
describe('facility repository rule test', async () => {
const facilityId = '0x1234567890';
const spaceId = '0x1234567890';
const facilityRuleRepository = new FacilityRuleRepository(facilityId);
Expand All @@ -18,8 +19,10 @@ describe('facility repository test', async () => {
});

it('get rule', async () => {
const rule = await facilityRuleRepository.getRule('notice_required') as NoticeRequiredRule;
const spaceRule = await spaceRuleRepository.getRule('notice_required') as NoticeRequiredRule;
const rule = await facilityRuleRepository.getRule('notice_required')as
NoticeRequiredRule;
const spaceRule = await spaceRuleRepository.getRule('notice_required') as
NoticeRequiredRule;

expect(rule.numDays).to.be.equal(10);
expect(spaceRule.numDays).to.be.equal(10);
Expand Down Expand Up @@ -48,3 +51,39 @@ describe('facility repository test', async () => {
expect(error.status).to.be.equal(404);
});
});

describe('repository rate test', async () => {
const facilityId = '0x1234567890';
const spaceId = '0x1234567890';
const spaceRuleRepository = new SpaceRateRepository(facilityId, spaceId);

it('set rate', async () => {
const rate: Rates = {
cost: 100,
includedOccupancy: 100
}

await spaceRuleRepository.setDefaultRate(rate);
});

it('get rate', async () => {
const spaceRate = await spaceRuleRepository.getRate('default');

expect(spaceRate?.cost).to.be.equal(100);
});

it('delete rate', async () => {
await spaceRuleRepository.delRate('default');
});

it('check rate not exist', async () => {
let error;

try {
await spaceRuleRepository.getRate('default');
} catch (e) {
error = e;
}
expect(error.status).to.be.equal(404);
});
});