175 lines
4.4 KiB
TypeScript
Raw Normal View History

2017-10-09 14:44:19 -07:00
import { faker } from 'ember-cli-mirage';
import { IFunctionRouteHandler, IMirageServer } from 'wherehows-web/typings/ember-cli-mirage';
import { ApiStatus } from 'wherehows-web/utils/api/shared';
import { getConfig } from 'wherehows-web/mirage/helpers/config';
import { getAuth } from 'wherehows-web/mirage/helpers/authenticate';
import { aclAuth } from 'wherehows-web/mirage/helpers/aclauth';
export default function(this: IMirageServer) {
this.get('/config', getConfig);
this.post('/authenticate', getAuth);
2017-10-09 14:44:19 -07:00
this.passthrough('/write-coverage');
this.namespace = '/api/v1';
this.get('/list/complianceDataTypes', function(
this: IFunctionRouteHandler,
{ complianceDataTypes }: { complianceDataTypes: any }
) {
return {
complianceDataTypes: this.serialize(complianceDataTypes.all()),
status: ApiStatus.OK
};
});
2017-10-09 14:44:19 -07:00
interface IFlowsObject {
flows: any;
}
this.get('/flows', function(this: IFunctionRouteHandler, { flows }: IFlowsObject, request: any) {
2017-10-09 14:44:19 -07:00
const { page } = request.queryParams;
const flowsArr = this.serialize(flows.all());
const count = faker.random.number({ min: 20000, max: 40000 });
const itemsPerPage = 10;
return {
2017-10-11 17:44:18 -07:00
status: ApiStatus.OK,
2017-10-09 14:44:19 -07:00
data: {
count: count,
flows: flowsArr,
itemsPerPage: itemsPerPage,
page: page,
totalPages: Math.round(count / itemsPerPage)
2017-10-09 14:44:19 -07:00
}
};
});
this.get('/list/datasets', (server: any) => {
const { datasetNodes } = server.db;
return {
status: 'ok',
nodes: datasetNodes
};
});
interface IOwnersObject {
owners: any;
}
interface IDatasetsObject {
datasets: any;
}
this.get('/datasets', function(
this: IFunctionRouteHandler,
{ datasets }: IDatasetsObject,
{ owners }: IOwnersObject,
request: any
) {
2017-10-09 14:44:19 -07:00
const { page } = request.queryParams;
const datasetsArr = this.serialize(datasets.all());
const ownersArr = this.serialize(owners.all());
const newDatasetsArr = datasetsArr.map(function(dataset: any) {
dataset.owners = ownersArr;
return dataset;
});
const count = faker.random.number({ min: 20000, max: 40000 });
const itemsPerPage = 10;
return {
2017-10-11 17:44:18 -07:00
status: ApiStatus.OK,
2017-10-09 14:44:19 -07:00
data: {
count: count,
page: page,
itemsPerPage: itemsPerPage,
totalPages: Math.round(count / itemsPerPage),
datasets: newDatasetsArr
2017-10-09 14:44:19 -07:00
}
};
});
this.get('/metrics', (server: any, request: any) => {
const { page } = request.queryParams;
const { metricMetrics } = server.db;
const count = faker.random.number({ min: 10000, max: 20000 });
const itemsPerPage = 10;
return {
2017-10-11 17:44:18 -07:00
status: ApiStatus.OK,
2017-10-09 14:44:19 -07:00
data: {
count: count,
page: page,
itemsPerPage: itemsPerPage,
totalPages: Math.round(count / itemsPerPage),
metrics: metricMetrics
}
};
});
this.get('/party/entities', (server: any) => {
const { userEntities } = server.db;
return {
2017-10-11 17:44:18 -07:00
status: ApiStatus.OK,
2017-10-09 14:44:19 -07:00
userEntities: userEntities
};
});
this.get('/user/me', () => {
const testUser = 'testUser';
return {
user: {
id: faker.random.number({ min: 1000, max: 5000 }),
userName: testUser,
departmentNum: 0,
email: testUser + '@linkedin.com',
name: testUser,
userSetting: <{ [prop: string]: null }>{
detailDefaultView: null,
defaultWatch: null
2017-10-09 14:44:19 -07:00
}
},
2017-10-11 17:44:18 -07:00
status: ApiStatus.OK
2017-10-09 14:44:19 -07:00
};
});
/**
* Add GET request to check the current user ACL permission
*/
this.get('/acl', (server: any, request: any) => {
if (request.queryParams.hasOwnProperty('LDAP')) {
const { LDAP } = request.queryParams;
const principal = `urn:li:userPrincipal:${LDAP}`;
let accessUserslist = server.db.datasetAclUsers.where({ principal });
if (accessUserslist.length > 0) {
return {
status: ApiStatus.OK,
isAccess: true,
body: server.db.datasetAclUsers
};
}
return {
status: ApiStatus.FAILED,
isAccess: false,
body: server.db.datasetAclUsers
};
} else {
return {
users: server.db.datasetAclUsers,
status: ApiStatus.OK
};
}
});
/**
* Add POST request to support to the current user get ACL permission.
*/
this.post('/acl', aclAuth);
2017-10-11 17:44:18 -07:00
this.passthrough();
}
export function testConfig(this: IMirageServer) {}