-
Notifications
You must be signed in to change notification settings - Fork 55
/
projectPhase.js
86 lines (79 loc) · 3.29 KB
/
projectPhase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import _ from 'lodash';
module.exports = function defineProjectPhase(sequelize, DataTypes) {
const ProjectPhase = sequelize.define('ProjectPhase', {
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING, allowNull: true },
description: { type: DataTypes.STRING, allowNull: true },
requirements: { type: DataTypes.STRING, allowNull: true },
status: { type: DataTypes.STRING, allowNull: true },
startDate: { type: DataTypes.DATE, allowNull: true },
endDate: { type: DataTypes.DATE, allowNull: true },
duration: { type: DataTypes.INTEGER, allowNull: true },
budget: { type: DataTypes.DOUBLE, defaultValue: 0.0 },
spentBudget: { type: DataTypes.DOUBLE, defaultValue: 0.0 },
progress: { type: DataTypes.DOUBLE, defaultValue: 0.0 },
details: { type: DataTypes.JSON, defaultValue: {} },
order: { type: DataTypes.INTEGER, allowNull: true },
deletedAt: { type: DataTypes.DATE, allowNull: true },
createdAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
updatedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
deletedBy: { type: DataTypes.INTEGER, allowNull: true },
createdBy: { type: DataTypes.INTEGER, allowNull: false },
updatedBy: { type: DataTypes.INTEGER, allowNull: false },
}, {
tableName: 'project_phases',
paranoid: true,
timestamps: true,
updatedAt: 'updatedAt',
createdAt: 'createdAt',
deletedAt: 'deletedAt',
indexes: [],
});
ProjectPhase.getActiveProjectPhases = projectId => ProjectPhase.findAll({
where: {
deletedAt: { $eq: null },
projectId,
},
raw: true,
});
ProjectPhase.associate = (models) => {
ProjectPhase.hasMany(models.PhaseProduct, { as: 'products', foreignKey: 'phaseId' });
ProjectPhase.hasMany(models.ProjectPhaseMember, { as: 'members', foreignKey: 'phaseId' });
ProjectPhase.hasMany(models.ProjectPhaseApproval, { as: 'approvals', foreignKey: 'phaseId' });
ProjectPhase.belongsToMany(models.WorkStream, { through: models.PhaseWorkStream, foreignKey: 'phaseId' });
};
/**
* Search project phases
* @param {Object} parameters the parameters
* - sortField: the field that will be references when sorting
* - sortType: ASC or DESC
* - fields: the fields to retrieved
* - projectId: the id of project
* @param {Object} log the request log
* @return {Object} the result rows and count
*/
ProjectPhase.search = async (parameters = {}, log) => {
// ordering
const orderBy = [];
if (_.has(parameters, 'sortField') && _.has(parameters, 'sortType')) {
orderBy.push([parameters.sortField, parameters.sortType]);
}
// find options
const options = {
where: {
projectId: parameters.projectId,
},
order: orderBy,
logging: (str) => { log.debug(str); },
};
// select fields
if (_.has(parameters, 'fields')) {
_.set(options, 'attributes', parameters.fields.filter(e => e !== 'products'));
if (parameters.fields.includes('products')) {
_.set(options, 'include', [{ model: this.sequelize.models.PhaseProduct, as: 'products' }]);
}
}
return ProjectPhase.findAll(options).then(phases => ({ rows: phases, count: phases.length }));
};
return ProjectPhase;
};