Skip to content
This repository was archived by the owner on Jun 6, 2023. It is now read-only.

WIP: simplify connecting device #202

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/models/device-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const schema = (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
})).required(),
})).required(),
commandTypeHashIds: Joi.array().items(Joi.string().example('x18a92')).required().description('The hashIds of the command types a user can schedule for this device'),
shimmable: Joi.boolean().required().example(false),
}).tag('deviceType')
.meta({ className: 'deviceType' })
.description('Information about the type of device');
Expand Down Expand Up @@ -64,6 +65,7 @@ interface DeviceType {
}[];
}[];
commandTypeHashIds: string[];
shimmable: boolean;
}

export {
Expand Down
20 changes: 20 additions & 0 deletions src/models/environment-webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Joi from 'joi';

const schema = Joi.object().keys({
deviceTypeId: Joi.string().required().example('xd2rd4'),
clientId: Joi.string().required().example('1'),
token: Joi.string().required().example('f8b0e692cb60c607e4eee49bde8552e0f09836079d0c714ef87536dc9a38e21fd06d0ff45bf8e2365c94d111800ec0ab8dd953bbdb823fec2c44959ed3a19df'),
createdAt: Joi.date().required().example('2022-05-17T12:34Z'),
})
.tag('environmentWebhook')
.meta({ className: 'environmentWebhook' })
.description('Shared device-type webhook');

interface EnvironmentWebhook {
deviceTypeId: string;
clientId: string;
token: string;
createdAt: Date;
}

export { schema, EnvironmentWebhook };
2 changes: 2 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as device from './device';
import * as deviceType from './device-type';
import * as edge from './edge';
import * as environment from './environment';
import * as environmentWebhook from './environment-webhook';
import * as environmentReportType from './environment-report-type';
import * as exportRequest from './export-request';
import * as fieldsFromServer from './fields/fields-from-server';
Expand Down Expand Up @@ -79,6 +80,7 @@ export {
deviceType,
edge,
environment,
environmentWebhook,
environmentReportType,
exportRequest,
fieldsFromServer,
Expand Down
2 changes: 2 additions & 0 deletions src/routes/device-type/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface Request {
}[];
}[];
commandTypeHashIds: string[];
shimmable: boolean;
};
}

Expand Down Expand Up @@ -74,6 +75,7 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
})).required(),
})),
commandTypeHashIds: Joi.array().items(Joi.string().example('x18a92')).required().description('The hashIds of the command types a user can schedule for this device'),
shimmable: Joi.boolean().required().example(false),
}).required(),
right: { supplier: 'ENVIRONMENT_ADMIN' },
response: Joi.object().keys({
Expand Down
2 changes: 2 additions & 0 deletions src/routes/device-type/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Response {
commandTypes: CommandType[];
subscriptionHashId?: string;
chartQuantities: Quantity[];
shimmable: boolean;
}

const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
Expand All @@ -33,6 +34,7 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
commandTypes: Joi.array().items(commandTypeSchema(apiVersion)).required(),
subscriptionHashId: Joi.string().description('If the user is subscribed to (email) alerts on this object, this key is present'),
chartQuantities: Joi.array().items(quantitySchema).required(),
shimmable: Joi.boolean().required().example(false),
}).required(),
description: 'Get a specific device type identified by its hashId',
};
Expand Down
2 changes: 2 additions & 0 deletions src/routes/device-type/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface Request {
}[];
}[];
commandTypeHashIds?: string[];
shimmable: boolean;
};
}

Expand Down Expand Up @@ -76,6 +77,7 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
})).required(),
})),
commandTypeHashIds: Joi.array().items(Joi.string()).description('The hashIds of the command types a user can schedule for this device'),
shimmable: Joi.boolean().required().example(false),
}).required(),
right: { supplier: 'ENVIRONMENT_ADMIN' },
description: 'Update the settings of a device type.',
Expand Down
34 changes: 34 additions & 0 deletions src/routes/environment-webhook/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Joi from 'joi';
import { ControllerGeneratorOptionsWithoutClientOrSupplier } from '../../comms/controller';

import { schema as environmentWebhookSchema, EnvironmentWebhook } from '../../models/environment-webhook';

interface Request {
params: {
deviceTypeHashId: string;
};
}

interface Response {
environmentWebhook: EnvironmentWebhook;
}

const controllerGeneratorOptions: ControllerGeneratorOptionsWithoutClientOrSupplier = {
method: 'get',
path: '/:deviceTypeHashId',
params: Joi.object().keys({
deviceTypeHashId: Joi.string().required().example('f1a4w1'),
}).required(),
right: {}, // supplierHashId in header is irrelevant
response: Joi.object().keys({
environmentWebhook: environmentWebhookSchema.required(),
}).required(),
description: 'Get a specific webhook shim identified by its device-type hashId',
};

export {
controllerGeneratorOptions,
Request,
Request as EffectiveRequest,
Response,
};
27 changes: 27 additions & 0 deletions src/routes/environment-webhook/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as get from './get';

import Comms from '../../comms';
import controllerGenerator, { Result } from '../../comms/controller';

class EnvironmentWebhookRoute {
static routerPath = 'environment-webhook';

static auth = true;

constructor(readonly comms: Comms) {
}

get = (parameters: get.Request):
Result<get.EffectiveRequest, get.Response> => controllerGenerator<
get.Request,
get.EffectiveRequest,
get.Response
>(
get.controllerGeneratorOptions,
EnvironmentWebhookRoute.routerPath,
EnvironmentWebhookRoute.auth,
this.comms,
)(parameters);
}

export default EnvironmentWebhookRoute;
5 changes: 5 additions & 0 deletions src/routes/environment-webhook/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as get from './get';

export {
get,
};
2 changes: 2 additions & 0 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as commandType from './command-type/routes';
import * as device from './device/routes';
import * as deviceType from './device-type/routes';
import * as environment from './environment/routes';
import * as environmentWebhook from './environment-webhook/routes';
import * as graph from './graph/routes';
import * as issue from './issue/routes';
import * as issueComment from './issue-comment/routes';
Expand Down Expand Up @@ -35,6 +36,7 @@ export {
device,
deviceType,
environment,
environmentWebhook,
graph,
issue,
issueComment,
Expand Down