diff --git a/lib/common/src/mongo.ts b/lib/common/src/mongo.ts index f99898b..be2518d 100644 --- a/lib/common/src/mongo.ts +++ b/lib/common/src/mongo.ts @@ -24,7 +24,7 @@ export async function configureMongoStores(readPreference = mongodb.ReadPreferen .then(() => { return { close: (): any => { - console.info("Disconnecting from mongodb..."); + console.info('Disconnecting from mongodb...'); return withCb(cb => mongoose.disconnect(cb)); } }; diff --git a/restclient.json b/restclient.json index e66895e..ef4c3bd 100644 --- a/restclient.json +++ b/restclient.json @@ -5,7 +5,7 @@ { "type": "Project", "id": "7b764e89-04c3-429d-96e1-38c3e8b73a92", - "lastModified": "2018-12-29T09:55:02.067+02:00", + "lastModified": "2018-12-29T23:02:50.697+02:00", "name": "Matilda" }, { @@ -40,9 +40,7 @@ "lastModified": "2018-12-29T09:55:02.067+02:00", "name": "GET http://localhost:3000/api/v1/tasks/id:task:1", "headers": [], - "metaInfo": { - "ownerId": "google-oauth2|106915669040165062216" - }, + "metaInfo": {}, "parentId": "7b764e89-04c3-429d-96e1-38c3e8b73a92" }, { @@ -77,30 +75,16 @@ "path": "/api/v1/tasks" }, "id": "3eea10ce-7024-40b7-9ea0-9d56ff395de6", - "lastModified": "2018-12-22T10:24:51.646+02:00", + "lastModified": "2018-12-29T23:02:50.698+02:00", "name": "POST http://localhost:3000/api/v1/tasks", "headers": [ - { - "name": "Authorization", - "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJub2VsQG1hbmFnZXIuY29tIiwianRpIjoiYTBmYjAxNjctZjZjYi00NmViLTgwYWUtMzZkNjA3MDZkZDg4IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiJmMTIzMjI5OS1hZjBiLTQwZTItODA4Yy04MGNiZDYzZDExYzEiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1pbiIsImV4cCI6MTUzNTYwOTUwNywiaXNzIjoibG9jYWxob3N0OjQ0MzMxIiwiYXVkIjoibG9jYWxob3N0OjQ0MzMxIn0._KwklIjp48k6wNLN64W9n6IKS47Nj1vekN7y4hg2Q7Q" - }, - { - "name": "X-Pagination-Page", - "value": "1" - }, - { - "name": "X-Pagination-Limit", - "value": "10" - }, { "enabled": true, "name": "Content-Type", "value": "application/json" } ], - "metaInfo": { - "ownerId": "google-oauth2|106915669040165062216" - }, + "metaInfo": {}, "parentId": "7b764e89-04c3-429d-96e1-38c3e8b73a92" }, { @@ -135,30 +119,16 @@ "path": "/api/v1/tasks/id:task:1" }, "id": "3d7aef01-15c9-48cb-b817-544d75029be1", - "lastModified": "2018-12-22T15:00:19.979+02:00", + "lastModified": "2018-12-29T23:02:46.829+02:00", "name": "PUT http://localhost:3000/api/v1/tasks/id:task:1", "headers": [ - { - "name": "Authorization", - "value": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJub2VsQG1hbmFnZXIuY29tIiwianRpIjoiYTBmYjAxNjctZjZjYi00NmViLTgwYWUtMzZkNjA3MDZkZDg4IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiJmMTIzMjI5OS1hZjBiLTQwZTItODA4Yy04MGNiZDYzZDExYzEiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJhZG1pbiIsImV4cCI6MTUzNTYwOTUwNywiaXNzIjoibG9jYWxob3N0OjQ0MzMxIiwiYXVkIjoibG9jYWxob3N0OjQ0MzMxIn0._KwklIjp48k6wNLN64W9n6IKS47Nj1vekN7y4hg2Q7Q" - }, - { - "name": "X-Pagination-Page", - "value": "1" - }, - { - "name": "X-Pagination-Limit", - "value": "10" - }, { "enabled": true, "name": "Content-Type", "value": "application/json" } ], - "metaInfo": { - "ownerId": "google-oauth2|106915669040165062216" - }, + "metaInfo": {}, "parentId": "7b764e89-04c3-429d-96e1-38c3e8b73a92" } ] diff --git a/src/controllers/HealthController.ts b/src/controllers/HealthController.ts index f03f0b4..6816576 100644 --- a/src/controllers/HealthController.ts +++ b/src/controllers/HealthController.ts @@ -1,4 +1,4 @@ -import { Router, Request, Response, NextFunction } from "express"; +import { Router, Request, Response, NextFunction } from 'express'; import { runRequest } from '@matilda/lib/common'; export class HealthController { @@ -19,11 +19,11 @@ export class HealthController { } /** - * Take each handler, and attach to one of the Express.Router"s + * Take each handler, and attach to one of the Express.Router's * endpoints. */ init() { - this.router.get("/", this.get); + this.router.get('/', this.get); } } diff --git a/src/controllers/TaskController.ts b/src/controllers/TaskController.ts index 7814b54..7195a33 100644 --- a/src/controllers/TaskController.ts +++ b/src/controllers/TaskController.ts @@ -45,14 +45,14 @@ export class TaskController { } /** - * Take each handler, and attach to one of the Express.Router"s + * Take each handler, and attach to one of the Express.Router's * endpoints. */ init() { - this.router.get("/:id", this.get); - this.router.post("/", this.post); - this.router.put("/:id", this.put); - this.router.delete("/:id", this.delete); + this.router.get('/:id', this.get); + this.router.post('/', this.post); + this.router.put('/:id', this.put); + this.router.delete('/:id', this.delete); } } diff --git a/src/index.ts b/src/index.ts index 43828ee..34f1697 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,9 +29,6 @@ function normalizePort(val: number | string): number | string | boolean { /** * Catch node errors for specific well known * errors. - * - * @param {NodeJS.ErrnoException} error - * @returns void */ function onError(error: NodeJS.ErrnoException): void { if (error.syscall !== 'listen') throw error; @@ -51,8 +48,6 @@ function onError(error: NodeJS.ErrnoException): void { } /** * We are alive - * - * @returns void */ function onListening(): void { let addr = server.address(); diff --git a/src/managers/TaskManager.ts b/src/managers/TaskManager.ts index faa7eb3..65adc6b 100644 --- a/src/managers/TaskManager.ts +++ b/src/managers/TaskManager.ts @@ -1,16 +1,25 @@ -import { TaskStore, ITaskStore } from '@matilda/src/stores/TaskStore'; +import { TaskMongoStore, ITaskStore } from '@matilda/src/stores'; import { throwError, ERRORS, payloadRequestToStoreRequest } from '@matilda/lib/common'; import { constructCreateTaskState, constructTaskState, ITaskStateParent } from '@matilda/src/states'; import { TaskData , CreateTaskPayload, UpdateTaskPayload, GetTaskPayload, DeleteTaskPayload } from '@matilda/src/types'; +import { ITaskManager } from './types'; -export class TaskManager { + + /** + * Task Manager manages the task logic + */ +export class TaskManager implements ITaskManager { private store: ITaskStore; + /** + * Initialize the Task Store + * @todo: Allow for use of different stores/databases + */ public constructor(){ - this.store = new TaskStore(); + this.store = new TaskMongoStore(); } - public async getTask(payload: GetTaskPayload): Promise { + public async getTask(payload: GetTaskPayload): Promise { const query = payloadRequestToStoreRequest({ id: payload.id }); query.filter.enabled = true; const tasks = await this.store.loadTasks(query).toArray(); @@ -24,6 +33,13 @@ export class TaskManager { return await this.store.createTask(state.task); } + /** + * Gets task with ID + * Constructs the task state + * Move to new state if needed + * Update Task sate + * Save to DB + */ public async updateTask(payload: UpdateTaskPayload): Promise { const task = await this.getTask(payload); if (!task) { @@ -34,12 +50,18 @@ export class TaskManager { if (task.status !== payload.task.status) { state = await state.moveTo(payload.task.status, payload.task); } - await state.update(payload.task); return await this.store.updateTask(state.task); } + /** + * Gets task with ID + * Constructs the task state + * Moves task to deleted state + * Update Task sate + * Save to DB + */ public async deleteTask(payload: DeleteTaskPayload): Promise { const task = await this.getTask(payload); if (!task) { @@ -59,10 +81,4 @@ export class TaskManager { } -export interface ITaskManager { - getTask(payload: GetTaskPayload): Promise - createTask(payload: CreateTaskPayload): Promise - updateTask(payload: UpdateTaskPayload): Promise - deleteTask(payload: DeleteTaskPayload): Promise -} diff --git a/src/managers/index.ts b/src/managers/index.ts index aea5761..aa7313e 100644 --- a/src/managers/index.ts +++ b/src/managers/index.ts @@ -1 +1,2 @@ -export * from './TaskManager'; \ No newline at end of file +export * from './TaskManager'; +export * from './types'; \ No newline at end of file diff --git a/src/managers/types.ts b/src/managers/types.ts new file mode 100644 index 0000000..346ed74 --- /dev/null +++ b/src/managers/types.ts @@ -0,0 +1,8 @@ +import { TaskData , CreateTaskPayload, UpdateTaskPayload, GetTaskPayload, DeleteTaskPayload } from '@matilda/src/types'; + +export interface ITaskManager { + getTask(payload: GetTaskPayload): Promise + createTask(payload: CreateTaskPayload): Promise + updateTask(payload: UpdateTaskPayload): Promise + deleteTask(payload: DeleteTaskPayload): Promise +} diff --git a/src/states/inprogress/InprogressDeinstallationTaskState.ts b/src/states/inprogress/InprogressDeinstallationTaskState.ts index 982498f..8d99da0 100644 --- a/src/states/inprogress/InprogressDeinstallationTaskState.ts +++ b/src/states/inprogress/InprogressDeinstallationTaskState.ts @@ -18,7 +18,7 @@ export class InprogressDeinstallationTaskState extends TaskState { break; default: - throwError(ERRORS.EINVALID, 'Illegal state transition from inprogress to ' + targetStatus); + throwError(ERRORS.EINVALID, `Illegal state transition from inprogress to ${targetStatus}`); } return await super.moveTo(targetStatus, data); diff --git a/src/states/inprogress/InprogressInstallationTaskState.ts b/src/states/inprogress/InprogressInstallationTaskState.ts index d0fc097..69db434 100644 --- a/src/states/inprogress/InprogressInstallationTaskState.ts +++ b/src/states/inprogress/InprogressInstallationTaskState.ts @@ -18,7 +18,7 @@ export class InprogressInstallationTaskState extends TaskState { break; default: - throwError(ERRORS.EINVALID, 'Illegal state transition from inprogress to ' + targetStatus); + throwError(ERRORS.EINVALID, `Illegal state transition from inprogress to ${targetStatus}`); } return await super.moveTo(targetStatus, data); diff --git a/src/states/inprogress/InprogressQuoteTaskState.ts b/src/states/inprogress/InprogressQuoteTaskState.ts index fc02cfa..35dedfd 100644 --- a/src/states/inprogress/InprogressQuoteTaskState.ts +++ b/src/states/inprogress/InprogressQuoteTaskState.ts @@ -18,7 +18,7 @@ export class InprogressQuoteTaskState extends TaskState { break; default: - throwError(ERRORS.EINVALID, 'Illegal state transition from inprogress to ' + targetStatus); + throwError(ERRORS.EINVALID, `Illegal state transition from inprogress to ${targetStatus}`); } return await super.moveTo(targetStatus, data); diff --git a/src/states/inprogress/InprogressRepairTaskState.ts b/src/states/inprogress/InprogressRepairTaskState.ts index 25b593c..f6c6769 100644 --- a/src/states/inprogress/InprogressRepairTaskState.ts +++ b/src/states/inprogress/InprogressRepairTaskState.ts @@ -18,7 +18,7 @@ export class InprogressRepairTaskState extends TaskState { break; default: - throwError(ERRORS.EINVALID, 'Illegal state transition from inprogress to ' + targetStatus); + throwError(ERRORS.EINVALID, `Illegal state transition from inprogress to ${targetStatus}`); } return await super.moveTo(targetStatus, data); diff --git a/src/stores/index.ts b/src/stores/index.ts new file mode 100644 index 0000000..00c0d10 --- /dev/null +++ b/src/stores/index.ts @@ -0,0 +1,2 @@ +export * from './types'; +export * from './task/TaskMongoStore'; \ No newline at end of file diff --git a/src/stores/TaskStore.ts b/src/stores/task/TaskMongoStore.ts similarity index 83% rename from src/stores/TaskStore.ts rename to src/stores/task/TaskMongoStore.ts index aa41772..74216ef 100644 --- a/src/stores/TaskStore.ts +++ b/src/stores/task/TaskMongoStore.ts @@ -1,10 +1,11 @@ import mongoose from 'mongoose'; -import { Cursor } from "mongodb"; +import { Cursor } from 'mongodb'; +import { ITaskStore } from '../types'; import { TaskData } from '@matilda/src/types'; import { findNextSequenceNumber, throwError, ERRORS, IMongoStoreQuery } from '@matilda/lib/common'; -export class TaskStore { +export class TaskMongoStore implements ITaskStore { public async createTask(task: TaskData): Promise { task.id = `id:task:${await findNextSequenceNumber(mongoose.connection, 'task_id')}`; @@ -50,9 +51,3 @@ export class TaskStore { } } - -export interface ITaskStore { - loadTasks(query: IMongoStoreQuery): Cursor - createTask(task: TaskData): Promise; - updateTask(task: TaskData): Promise; -} diff --git a/src/stores/types.ts b/src/stores/types.ts new file mode 100644 index 0000000..b934adc --- /dev/null +++ b/src/stores/types.ts @@ -0,0 +1,9 @@ +import { Cursor } from 'mongodb'; +import { TaskData } from '@matilda/src/types'; +import { IMongoStoreQuery } from '@matilda/lib/common'; + +export interface ITaskStore { + loadTasks(query: IMongoStoreQuery): Cursor + createTask(task: TaskData): Promise; + updateTask(task: TaskData): Promise; +}