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
16 changes: 6 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@
"luxon": "^2.4.0",
"morgan": "^1.10.0",
"multer": "^1.4.4",
"path": "^0.12.7",
"prom-client": "^14.0.1",
"response-time": "^2.3.2",
"sublevel": "^2.4.0",
"url": "^0.11.0",
"web3.storage": "^4.2.0"
},
"devDependencies": {
Expand All @@ -85,18 +82,18 @@
"@types/mocha": "^9.1.1",
"@types/morgan": "^1.9.3",
"@types/multer": "^1.4.7",
"@types/node": "^17.0.38",
"@types/node": "^17.0.41",
"@types/response-time": "^2.3.5",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"@windingtree/videre-contracts": "^1.0.2",
"c8": "^7.11.3",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"chai-ethers": "^0.0.1",
"conventional-changelog-cli": "^2.2.2",
"eslint": "^8.16.0",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
Expand All @@ -106,17 +103,16 @@
"eslint-plugin-promise": "^6.0.0",
"git-cz": "^4.9.0",
"husky": "^8.0.1",
"kleur": "^4.1.4",
"lint-staged": ">=10",
"mocha": "^10.0.0",
"nodemon": "^2.0.16",
"prettier": "^2.6.2",
"supertest": "^6.2.3",
"swagger-jsdoc": "^6.2.1",
"swagger-ui-express": "^4.4.0",
"ts-node": "^10.8.0",
"ts-node": "^10.8.1",
"typechain": "^8.0.0",
"typescript": "^4.7.2"
"typescript": "^4.7.3"
},
"lint-staged": {
"*.{ts,js,sol,json}": [
Expand Down
32 changes: 22 additions & 10 deletions src/controllers/FacilityController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ export class FacilityController {
const { facilityId, spaceId, date } = req.params;

const repository = new SpaceAvailabilityRepository(facilityId, spaceId);
const numSpaces = await repository.getSpaceAvailability(
const availability = await repository.getSpaceAvailability(
date as FormattedDate
);

return res.json({ numSpaces });
if (!availability) {
throw ApiError.NotFound(
`Unable to get availability of space: ${spaceId} of the facility: ${facilityId}`
);
}

return res.json(availability);
} catch (e) {
next(e);
}
Expand Down Expand Up @@ -92,6 +98,12 @@ export class FacilityController {
modifierKey as ModifiersKey
);

if (!modifier) {
throw ApiError.NotFound(
`Unable to get "${modifierKey}" of the facility: ${facilityId}`
);
}

res.json(modifier);
} catch (e) {
next(e);
Expand All @@ -113,22 +125,22 @@ export class FacilityController {
itemId
);

let modifier: ModifiersValues;

try {
modifier = await repository.getModifier(modifierKey as ModifiersKey);
} catch (e) {
if (e.status !== 404) {
throw e;
}
let modifier = await repository.getModifier(modifierKey as ModifiersKey);

if (!modifier) {
// If item does not contain such modifier then try to lookup the facility
const facilityRepository = new FacilityModifierRepository(facilityId);
modifier = await facilityRepository.getModifier(
modifierKey as ModifiersKey
);
}

if (!modifier) {
throw ApiError.NotFound(
`Unable to get "${modifierKey}" of the "${itemKey}": ${itemId} of the facility: ${facilityId}`
);
}

res.json(modifier);
} catch (e) {
next(e);
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { port, prometheusEnabled } from './config';
import bootstrapService from './services/BootstrapService';
import DBService from './services/DBService';
import { MetricsService } from './services/MetricsService';
import WakuService from './services/WakuService';
// import WakuService from './services/WakuService';

process.on('unhandledRejection', async (error) => {
console.log(error);
Expand All @@ -13,7 +13,7 @@ process.on('unhandledRejection', async (error) => {

const main = async (): Promise<ServerService> => {
const server = new ServerService(port);
const waku = WakuService.getInstance;
// const waku = WakuService.getInstance;

await bootstrapService.bootstrap();

Expand Down
31 changes: 13 additions & 18 deletions src/repositories/FacilityRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import DBService, {
} from '../services/DBService';
import { Level } from 'level';
import { Item } from '../proto/facility';
import ApiError from '../exceptions/ApiError';

export class FacilityRepository {
private dbService: DBService;
Expand Down Expand Up @@ -65,20 +64,18 @@ export class FacilityRepository {
await this.dbService.getFacilityDB(facilityId).put(key, value);
}

public async getFacilityKey(
public async getFacilityKey<T extends FacilityValues>(
facilityId: string,
key: FacilityKey | FacilityIndexKey
): Promise<FacilityValues> {
): Promise<T | null> {
try {
return await this.dbService.getFacilityDB(facilityId).get(key);
return (await this.dbService.getFacilityDB(facilityId).get(key)) as T;
} catch (e) {
if (e.status === 404) {
throw ApiError.NotFound(
`Unable to get "${key}" of facility "${facilityId}"`
);
if (e.status !== 404) {
throw e;
}
throw e;
}
return null;
}

public async delFacilityKey(
Expand Down Expand Up @@ -154,24 +151,22 @@ export class FacilityRepository {
.put(key, value);
}

public async getItemKey(
public async getItemKey<T extends Item | FacilitySpaceValues>(
facilityId: string,
idx: FacilityIndexKey,
itemId: string,
key: string
): Promise<Item | FacilitySpaceValues> {
): Promise<T | null> {
try {
return await this.dbService
return (await this.dbService
.getFacilityItemDB(facilityId, idx, itemId)
.get(key);
.get(key)) as T;
} catch (e) {
if (e.status === 404) {
throw ApiError.NotFound(
`Unable to get "${key}" of item "${itemId}" of facility "${facilityId}"`
);
if (e.status !== 404) {
throw e;
}
throw e;
}
return null;
}

public async delItemKey(
Expand Down
7 changes: 3 additions & 4 deletions src/repositories/ItemRateRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import DBService, {
FormattedDate,
LevelDefaultTyping
} from '../services/DBService';
import ApiError from '../exceptions/ApiError';
import { AbstractSublevel } from 'abstract-level';
import { Rates } from '../proto/lpms';

Expand Down Expand Up @@ -37,11 +36,11 @@ abstract class ItemRateRepository {
try {
return await this.db.get(key);
} catch (e) {
if (e.status === 404) {
throw ApiError.NotFound(`Unable to get "${key}" of rate level"`);
if (e.status !== 404) {
throw e;
}
throw e;
}
return null;
}

public async setRate(key: FormattedDate, value: Rates): Promise<void> {
Expand Down
13 changes: 7 additions & 6 deletions src/repositories/ModifierRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AbstractSublevel } from 'abstract-level';
import ApiError from '../exceptions/ApiError';
import DBService, {
DBLevel,
LevelDefaultTyping,
Expand All @@ -14,15 +13,17 @@ abstract class ModifierRepository {
protected dbService: DBService = DBService.getInstance();
protected db;

public async getModifier(key: ModifiersKey): Promise<ModifiersValues> {
public async getModifier<T extends ModifiersValues>(
key: ModifiersKey
): Promise<T | null> {
try {
return await this.db.get(key);
return (await this.db.get(key)) as T;
} catch (e) {
if (e.status === 404) {
throw ApiError.NotFound(`Unable to get "${key}" of modifier level"`);
if (e.status !== 404) {
throw e;
}
throw e;
}
return null;
}

public async setModifier(
Expand Down
4 changes: 2 additions & 2 deletions src/repositories/RuleRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ abstract class RuleRepository {
protected db;
protected dbService = DBService.getInstance();

public async getRule(key: RulesItemKey): Promise<Rules | null> {
public async getRule<T extends Rules>(key: RulesItemKey): Promise<T | null> {
try {
return await this.db.get(key);
return (await this.db.get(key)) as T;
} catch (e) {
if (e.status !== 404) {
throw e;
Expand Down
7 changes: 2 additions & 5 deletions src/repositories/SpaceAvailabilityRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ export class SpaceAvailabilityRepository {

public async getSpaceAvailability(
key: DefaultOrDateItemKey
): Promise<Availability> {
): Promise<Availability | null> {
try {
return await this.db.get(key);
} catch (e) {
if (e.status !== 404) {
throw e;
}
}

return {
numSpaces: 0
};
return null;
}

public async setAvailabilityDefault(
Expand Down
1 change: 0 additions & 1 deletion src/repositories/SpaceStubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class SpaceStubRepository extends AbstractStubRepository {
throw e;
}
}

return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions src/repositories/TokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export class TokenRepository {
try {
return await this.db.get(String(userId));
} catch (e) {
if (e.status === 404) {
return [];
if (e.status !== 404) {
throw e;
}
throw e;
}
return [];
}

public async setUserTokens(
Expand Down
1 change: 1 addition & 0 deletions src/services/AuctioneerService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class AuctioneerService {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(where: string[]) {
// Listen to waku and contentTopics from where when listening for a an Ask.
// Monitor Stays events, filtering by facilityId on chain for any Deals that have been done.
Expand Down
20 changes: 14 additions & 6 deletions src/services/PingPongService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import log from './LogService';
import WakuService from './WakuService';
import WalletService from './WalletService';

import { walletAccountsIndexes } from 'src/types';
import { Facility } from 'src/proto/facility';
import { Ping, Pong } from 'src/proto/pingpong';
import { LatLng } from 'src/proto/latlng';
import { walletAccountsIndexes } from '../types';
import { Facility } from '../proto/facility';
import { Ping, Pong } from '../proto/pingpong';
import { LatLng } from '../proto/latlng';

import { typedDataDomain, videreConfig } from '../config';
import ApiError from '../exceptions/ApiError';

export default class PingPongService {
protected waku: WakuService;
Expand All @@ -28,10 +29,17 @@ export default class PingPongService {
* for a specified facility.
*/
public async start(facilityId: string): Promise<void> {
const metadata: Facility = (await facilityRepository.getFacilityKey(
const metadata = await facilityRepository.getFacilityKey<Facility>(
facilityId,
'metadata'
)) as Facility;
);

if (metadata === null) {
throw ApiError.NotFound(
`Unable to find "metadata" of the facility: ${facilityId}`
);
}

const loc = metadata.location;

if (!this.waku) this.waku = WakuService.getInstance();
Expand Down
Loading