Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge a952d27 into 8971139
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobueno committed Sep 21, 2017
2 parents 8971139 + a952d27 commit 18a3309
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 6 deletions.
13 changes: 13 additions & 0 deletions client/wfm/src/model/WorkOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@ export interface WorkOrder {
* Timestamp of this WorkOrder's last update
*/
updated: number;

/**
* List of timestamps of when this WorkOrder last reached a given {@link STATUS}
*
* @example
* {
* 'New': 1506010024468,
* 'Complete': 1506010024468
* }
*/
statusHistory?: {
[status: string]: number
};
}
12 changes: 9 additions & 3 deletions client/wfm/src/service/WfmService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class WfmService {
if (!workorder.assignee) {
throw new Error(`Workorder with Id ${workorder.id} has no assignee and cannot be started`);
}
workorder.status = STATUS.PENDING;
this.setWorkOrderStatus(workorder, STATUS.PENDING);
workorder.currentStep = workorder.workflow.steps[0].id;
workorder.results = [];
return this.workorderService.update(workorder);
Expand Down Expand Up @@ -94,7 +94,7 @@ export class WfmService {
}
// if there's no longer a current step, we've backed into NEW status
if (!workorder.currentStep) {
workorder.status = STATUS.NEW;
this.setWorkOrderStatus(workorder, STATUS.NEW);
}

return this.workorderService.update(workorder);
Expand Down Expand Up @@ -188,10 +188,16 @@ export class WfmService {
}
// if current is the last, workorder is now complete
if (index === workorder.workflow.steps.length - 1) {
workorder.status = STATUS.COMPLETE;
this.setWorkOrderStatus(workorder, STATUS.COMPLETE);
return workorder.currentStep = undefined;
}
const nextStep = workorder.workflow.steps[index + 1];
return workorder.currentStep = nextStep && nextStep.id;
}

protected setWorkOrderStatus(workorder: WorkOrder, status: STATUS) {
workorder.statusHistory = workorder.statusHistory || {};
workorder.statusHistory[status] = new Date().getTime();
workorder.status = status;
}
}
2 changes: 2 additions & 0 deletions client/wfm/test/service/WfmService-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('WfmService', function() {
.then(workorder => subject.begin(workorder))
.then(workorder => {
expect(workorder.status).to.equal(STATUS.PENDING);
// tslint:disable-next-line:no-unused-expression
expect(workorder.statusHistory && workorder.statusHistory[STATUS.PENDING]).to.exist;
expect(workorder.currentStep).to.equal(workorder.workflow.steps[0].id);
});
});
Expand Down
3 changes: 2 additions & 1 deletion demo/server/config-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ var config = {
},
"sync": {
// Required to handle UI.
"customDataHandlers": true
"customDataHandlers": true,
"excludeOldCompleteWorkOrders": 2
},
// See bunyan.d.ts/LoggerOptions
"bunyanConfig": {
Expand Down
3 changes: 2 additions & 1 deletion demo/server/config-prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ var config = {
},
"sync": {
// Required to handle UI.
"customDataHandlers": true
"customDataHandlers": true,
"excludeOldCompleteWorkOrders": 2
},
// See bunyan.d.ts/LoggerOptions
"bunyanConfig": {
Expand Down
7 changes: 6 additions & 1 deletion demo/server/src/modules/datasync/Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SyncServer, { SyncApi, SyncExpressMiddleware, SyncOptions } from '@rainca
import { getLogger } from '@raincatcher/logger';
import * as Promise from 'bluebird';
import appConfig from '../../util/Config';
import { excludeCompleteWorkOrders } from './filters/ExcludeCompletedWorkorders';
import { GlobalMongoDataHandler } from './MongoDataHandler';

const sync = SyncServer;
Expand All @@ -13,7 +14,7 @@ process.env.DEBUG = 'fh-mbaas-api:sync';
// Sync connection options
const connectOptions: SyncOptions = {
datasetConfiguration: {
mongoDbConnectionUrl: appConfig.getConfig().mongodb.url,
mongoDbConnectionUrl: appConfig.getConfig().mongodb.url,
mongoDbOptions: appConfig.getConfig().mongodb.options,
redisConnectionUrl: appConfig.getConfig().redis.url
},
Expand All @@ -37,6 +38,10 @@ export function connect() {
}
if (config.sync.customDataHandlers) {
const handler = new GlobalMongoDataHandler(mongo);
const excludeDays = appConfig.getConfig().sync.excludeOldCompleteWorkOrders;
if (excludeDays > 0) {
handler.addListFilterModifier(excludeCompleteWorkOrders(excludeDays));
}
handler.initGlobalHandlers();
}
return resolve({ mongo, redis });
Expand Down
14 changes: 14 additions & 0 deletions demo/server/src/modules/datasync/MongoDataHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { sync } from '@raincatcher/datasync-cloud';
import { Db } from 'mongodb';

type FilterModifier = (queryParams: any) => void;

/**
* Initializes global mongodb data handlers for feedhenry sync
* This class override default data handlers in sync to provide more flexible way of handling data.
Expand All @@ -9,12 +11,21 @@ import { Db } from 'mongodb';
* In this datahandler we migrating from ObjectId identifiers to client generated ids.
*/
export class GlobalMongoDataHandler {
private listFilterModifiers: FilterModifier[] = [];
/**
* @param db MongoDb connection
*/
constructor(private db: Db) {
}

/**
* Adds a modifier to a queue that can mutate the parameters used in the base List query for MongoDB
* @param modifier A function that mutates the `queryParameters` object synchronously
*/
public addListFilterModifier(modifier: FilterModifier) {
this.listFilterModifiers.push(modifier);
}

/**
* Init all setupHandlers for CRUD operations
*/
Expand All @@ -30,6 +41,9 @@ export class GlobalMongoDataHandler {
const self = this;
sync.globalHandleList(function(datasetId, queryParams, metadata, cb) {
queryParams = queryParams || {};

self.listFilterModifiers.forEach(modifier => modifier(queryParams));

const resultPromise = self.db.collection(datasetId).find(queryParams);
return resultPromise.toArray().then(function(list: any[]) {
return cb(undefined, self.toObject(list));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Excludes WorkOrders with a Complete date over a set number of days ago
* @param queryParams
*/
export function excludeCompleteWorkOrders(days: number) {
if (days < 0) {
throw new Error(`Days must be a number greater than or equal to zero, got ${days}`);
}
return function(queryParams: any) {
const daysAgo = new Date();
daysAgo.setDate(daysAgo.getDate() - days);
const excludeQuery = {
'$or': [
{ 'statusHistory.Complete': { '$exists': false } },
{ 'statusHistory.Complete': { '$gt': daysAgo.getTime() } }
]
};

// Add this query to $and so it doesn't overwrite other queries
queryParams.$and = queryParams.$and || [];
queryParams.$and.push(excludeQuery);
};
}
3 changes: 3 additions & 0 deletions demo/server/src/modules/datasync/filters/filterModifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type FilterModifier {
(params: any): any
}
7 changes: 7 additions & 0 deletions demo/server/src/util/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export interface CloudAppConfig {
};
sync: {
customDataHandlers: boolean;
/**
* Specify that completed WorkOrders over a given number of days must be excluded from listings
* sent to mobile clients
*
* Only works if {@link customDataHandlers} is true
*/
excludeOldCompleteWorkOrders: number;
globalOptions: SyncGlobalParameters;
};
mongodb: {
Expand Down

0 comments on commit 18a3309

Please sign in to comment.