Skip to content

Commit

Permalink
Added comments and some general clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKriegler committed Dec 29, 2018
1 parent 7903737 commit a54ebbf
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 74 deletions.
2 changes: 1 addition & 1 deletion lib/common/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
Expand Down
42 changes: 6 additions & 36 deletions restclient.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Expand Down Expand Up @@ -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"
},
{
Expand Down Expand Up @@ -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"
},
{
Expand Down Expand Up @@ -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"
}
]
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/HealthController.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/controllers/TaskController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,8 +48,6 @@ function onError(error: NodeJS.ErrnoException): void {
}
/**
* We are alive
*
* @returns void
*/
function onListening(): void {
let addr = server.address();
Expand Down
38 changes: 27 additions & 11 deletions src/managers/TaskManager.ts
Original file line number Diff line number Diff line change
@@ -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<TaskData|null> {
public async getTask(payload: GetTaskPayload): Promise<TaskData> {
const query = payloadRequestToStoreRequest({ id: payload.id });
query.filter.enabled = true;
const tasks = await this.store.loadTasks(query).toArray();
Expand All @@ -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<TaskData> {
const task = await this.getTask(payload);
if (!task) {
Expand All @@ -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<TaskData> {
const task = await this.getTask(payload);
if (!task) {
Expand All @@ -59,10 +81,4 @@ export class TaskManager {

}

export interface ITaskManager {
getTask(payload: GetTaskPayload): Promise<TaskData|null>
createTask(payload: CreateTaskPayload): Promise<TaskData>
updateTask(payload: UpdateTaskPayload): Promise<TaskData>
deleteTask(payload: DeleteTaskPayload): Promise<TaskData>
}

3 changes: 2 additions & 1 deletion src/managers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './TaskManager';
export * from './TaskManager';
export * from './types';
8 changes: 8 additions & 0 deletions src/managers/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TaskData , CreateTaskPayload, UpdateTaskPayload, GetTaskPayload, DeleteTaskPayload } from '@matilda/src/types';

export interface ITaskManager {
getTask(payload: GetTaskPayload): Promise<TaskData>
createTask(payload: CreateTaskPayload): Promise<TaskData>
updateTask(payload: UpdateTaskPayload): Promise<TaskData>
deleteTask(payload: DeleteTaskPayload): Promise<TaskData>
}
2 changes: 1 addition & 1 deletion src/states/inprogress/InprogressDeinstallationTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/states/inprogress/InprogressInstallationTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/states/inprogress/InprogressQuoteTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/states/inprogress/InprogressRepairTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './task/TaskMongoStore';
11 changes: 3 additions & 8 deletions src/stores/TaskStore.ts → src/stores/task/TaskMongoStore.ts
Original file line number Diff line number Diff line change
@@ -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<TaskData> {
task.id = `id:task:${await findNextSequenceNumber(mongoose.connection, 'task_id')}`;
Expand Down Expand Up @@ -50,9 +51,3 @@ export class TaskStore {
}

}

export interface ITaskStore {
loadTasks(query: IMongoStoreQuery): Cursor<TaskData>
createTask(task: TaskData): Promise<TaskData>;
updateTask(task: TaskData): Promise<TaskData>;
}
9 changes: 9 additions & 0 deletions src/stores/types.ts
Original file line number Diff line number Diff line change
@@ -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<TaskData>
createTask(task: TaskData): Promise<TaskData>;
updateTask(task: TaskData): Promise<TaskData>;
}

0 comments on commit a54ebbf

Please sign in to comment.