Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {OomnitzaDb} from "../api/api.coffee";
- import {OomnitzaUtility} from "../util/util.coffee";
- import moment from "moment";
- import _ from "lodash";
- import {ARCHIVE, EDIT, NEW, VERSION, VIEW} from "./constants";
- const db = new OomnitzaDb();
- const getContractMedia = (contract_id) => {
- return db.v3.query.get(["contracts", contract_id, "media"]);
- };
- const sendContractMedia = (contract_id, data) => {
- return db.v3.query.postFormData(["contracts", contract_id, "media"], data);
- };
- const deleteContractMedia = (contract_id, media_id) => {
- return db.v3.query.delete(["contracts", contract_id, "media", media_id]);
- };
- const setAsContractDisplayPhoto = (contract_id, media_id) => {
- return db.v3.query.patch(["contracts", contract_id, "media", media_id, "default_display"], {});
- };
- const getHistoryList = (contract_id, skipCount, params) => {
- const queryParams = ["limit=100", `skip=${skipCount}`, ...params];
- return db.v3.query.get(["contracts", contract_id, "changes-history"], queryParams);
- };
- const getContractMeta = () => {
- return db.v3.query.meta("contracts");
- };
- const getContractDetails = (id) => {
- return db.v3.query.get(["contracts", id]);
- };
- const getContractFromArchive = (id) => {
- return db.v3.query.get(["contracts", id], ["include_deleted=1"]);
- };
- const getContractFromHistory = (id, version) => {
- return db.v3.query.get(["contracts", id, "history", version]);
- };
- const restoreContract = (id) => {
- return db.v3.query.patch(["contracts", id, "?include_deleted=1"], {deleted: null});
- };
- function searchUsers(value) {
- return db.v3.query.get(["users", "suggest"], ["q=" + value]);
- }
- function searchLocations(value) {
- return db.v3.query.get(["locations", "suggest"], ["q=" + value]);
- }
- function searchSaas(value) { //TODO
- return db.v3.query.get(["saas", "suggest"], ["q=" + value]);
- }
- function searchContractField(fieldId, value) {
- return db.v3.query.get(["contracts", "autocomplete", fieldId], ["q=" + value]);
- }
- const onAutocomplete = (fieldId, value, data_type) => {
- if (data_type === "USERS") {
- return searchUsers(value);
- } else if (data_type === "LOCATION") {
- return searchLocations(value);
- } else if (data_type === "SAAS") { //TODO
- return searchSaas(value);
- }
- return searchContractField(fieldId, value);
- };
- const updateContract = (contrac_id, data) => {
- return db.v3.query.patch(["contracts", contrac_id], data);
- };
- const createContract = (data) => {
- return db.v3.query.post(["contracts"], data);
- };
- const wfTriggerCheck = (contract_id, data, mode = "EDIT") => {
- return db.v3.query[mode == NEW ? "post" : "patch"](["contracts", contract_id, "wf-trigger"], data);
- };
- // Link to description https://oomnitza.atlassian.net/wiki/spaces/OPS/pages/1196425364/Object+Contract#id-[Object]Contract-FieldsOrder
- const defaultContractFieldsConfig = {
- fields: [
- "contract_name",
- "contract_type",
- "software_id",
- "payment_date",
- "payment_schedule",
- "renewal_date",
- "price",
- "responsible_person",
- "limit_number_of_licenses",
- "number_of_licenses",
- "number_of_seats",
- "license_key",
- "calculate_per_user_price",
- "version",
- "change_date",
- "changed_by",
- "creation_date",
- "created_by",
- "start_date",
- "old_renewal_date",
- "contract_end_date",
- "vendor_name",
- "support_phone",
- "contract_id",
- "saas_id",
- "latest_payment_date"
- ],
- fields_enable: {
- "contract_name": true,
- "contract_type": true,
- "software_id": true,
- "payment_date": true,
- "payment_schedule": true,
- "renewal_date": true,
- "price": true,
- "responsible_person": true,
- "limit_number_of_licenses": true,
- "number_of_licenses": true,
- "number_of_seats": true,
- "license_key": true,
- "calculate_per_user_price": true,
- "version": true,
- "change_date": true,
- "changed_by": true,
- "creation_date": true,
- "created_by": true,
- "start_date": true,
- "old_renewal_date": true,
- "contract_end_date": true,
- "vendor_name": true,
- "support_phone": true,
- "contract_id": true,
- "saas_id": true,
- "latest_payment_date": true
- }
- };
- function getInitialVisibleFields() {
- return {...defaultContractFieldsConfig};
- }
- function getRoleConfig() {
- return db.v3.query.get(["service", "profile"]);
- }
- function getSBConfigList() {
- return db.v3.query.get(["settings", "role_based_views"]);
- }
- function getSBConfigById(id) {
- return db.v3.query.get(["settings", "role_based_views", id]);
- }
- function getFieldsConfiguredOnSB(belongs, defaultFields) {
- return $.when(
- getRoleConfig(),
- getSBConfigList()
- )
- .then(([{role}], [views]) => {
- const view = _.find(views, {belongs_to: belongs, permissions_id: role});
- return view ? getSBConfigById(view.id) : defaultFields;
- })
- .catch(() => defaultFields);
- }
- const fieldVisibleByConfig = (fields, field, contract_type, limit) => {
- const ids = ["number_of_licenses"];
- if (contract_type === ENTERPRISE_CONTRACT && _.includes(ids, field) && limit != "1") {
- return false;
- }
- return !_.has(fields, field) || fields[field];
- };
- const hiddenFields = ["uid", "version"];
- const contractAttributes = [
- "contract_name",
- "contract_type",
- "saas_id",
- "software_id",
- "payment_date",
- "payment_schedule",
- "latest_payment_date",
- "renewal_date",
- "price",
- "responsible_person",
- "limit_number_of_licenses",
- "calculate_per_user_price",
- "number_of_licenses",
- "number_of_seats",
- "license_key"
- ];
- const REGULAR_CONTRACT = "Regular Contract";
- const ENTERPRISE_CONTRACT = "SaaS \"Enterprise\" Contract";
- const PER_USER_CONTRACT = "SaaS \"Per User\" Contract";
- const DESKTOP_SOFTWARE_CONTRACT = "Desktop Software Contract";
- const contractTypes = [REGULAR_CONTRACT, PER_USER_CONTRACT, ENTERPRISE_CONTRACT, DESKTOP_SOFTWARE_CONTRACT];
- const typeToAttribute = {
- [REGULAR_CONTRACT]: [
- "contract_name",
- "contract_type"
- ],
- [PER_USER_CONTRACT]: [
- "contract_name",
- "contract_type",
- "saas_id",
- "software_id",
- "payment_date",
- "payment_schedule",
- "latest_payment_date",
- "renewal_date",
- "price",
- "responsible_person",
- "number_of_licenses"
- ],
- [ENTERPRISE_CONTRACT]: [
- "contract_name",
- "contract_type",
- "saas_id",
- "software_id",
- "payment_date",
- "payment_schedule",
- "latest_payment_date",
- "renewal_date",
- "price",
- "responsible_person", "limit_number_of_licenses",
- "calculate_per_user_price",
- "number_of_licenses"],
- [DESKTOP_SOFTWARE_CONTRACT]: [
- "contract_name",
- "contract_type",
- "software_id",
- "payment_date",
- "payment_schedule",
- "latest_payment_date",
- "renewal_date",
- "price",
- "responsible_person",
- "calculate_per_user_price",
- "number_of_seats",
- "license_key"
- ]
- };
- const isContractAttribute = (field_id) => {
- return _.includes(contractAttributes, field_id);
- };
- const isRelevantContractField = (contract_type, field_id) => {
- if (!_.includes(contractTypes, contract_type)) {
- return _.includes(["contract_name", "contract_type"], field_id);
- }
- return !isContractAttribute(field_id) || _.includes(typeToAttribute[contract_type], field_id);
- };
- function isAutocomplete(type) {
- return _.includes(["USERS", "LOCATION"], type);
- }
- function isCustomAutocomplete(searchhelp_type) {
- return searchhelp_type === "dynamic";
- }
- function fieldNotVisible(meta, id, data) {
- return !isRelevantContractField(data.contract_type, id) && !shouldShowByRelational(meta, id, data);
- }
- function isEditable(val, mode, id, data_type) {
- const ids = ["contract_type"];
- if (mode === VIEW) {
- return false;
- } else if (mode === ARCHIVE) {
- return false;
- } else if (mode === VERSION) {
- return false;
- } else if (mode === NEW && _.includes(ids, id)) {
- return true;
- }
- if (_.includes(["SAAS"], data_type)) {
- return true;
- }
- return val;
- }
- const isEmptyVal = (val) => {
- if (_.isObject(val)) {
- return _.isEmpty(val) || _.isEqual(val, {id: null, label: null});
- }
- if (_.isString(val)) {
- return !_.trim(val).length;
- }
- return _.isNil(val);
- };
- const isMandatory = (meta, id) => {
- return _.has(meta, id) && !formatBool(_.get(meta, [id, "optional"], false));
- };
- const isMandatoryEmpty = (meta, id, data) => {
- return isMandatory(meta, id) && isEmptyVal(data[id]);
- };
- const shouldShowByRelational = (meta, id, data) => {
- let dependencies = _.get(meta, [id, "dependencies"], {});
- if (!_.isEmpty(dependencies)) {
- return _.every(dependencies, (values, id) => {
- let {data_type} = meta[id];
- let value = data[id];
- if (data_type === "LOCATION") {
- return _.includes(values, _.get(value, "value", value));
- }
- return _.includes(values, value);
- });
- }
- return true;
- };
- const shouldShowInMode = (mode, field_id, editable) => {
- let isCustomField = !isContractAttribute(field_id);
- if (mode === NEW && isCustomField) {
- return formatBool(editable);
- }
- return true;
- };
- const hasChangesToSave = (prevdata, data) => {
- return !_.every(data, (value, id) => {
- if ((isEmptyVal(value) && isEmptyVal(prevdata[id]))) {
- return true;
- }
- return _.isEqual(value, prevdata[id]);
- });
- };
- function formatBool(val) {
- return val == "1";
- }
- function getErrorsModel(meta) {
- let errorModel = {};
- _.map(_.keys(_.omit(meta, hiddenFields)), (key) => {
- errorModel[key] = "";
- });
- return errorModel;
- }
- const getInitialContractDetails = (meta) => {
- return _.mapValues(meta, ({default_value}) => default_value);
- };
- function getFieldList(meta, data, mode, {fields_enable, fields}) {
- const newMeta = _.omit(meta, hiddenFields);
- const contract_type = data.contract_type;
- const limit = _.get(data, "limit_number_of_licenses", false);
- let fieldList = _.map(newMeta, ({data_type, editable, optional, hardcode, label, length, searchhelp, searchhelp_type, display_order, dependencies}, id) => {
- return {
- id,
- data_type,
- label,
- searchhelp,
- searchhelp_type,
- dependencies,
- editable: isEditable(formatBool(editable), mode, id, data_type),
- mandatory: !formatBool(optional),
- hasSystemLogo: formatBool(hardcode),
- maxLength: length,
- order: display_order,
- value: getValue(data[id], data_type)
- };
- })
- .filter(({id}) => isRelevantContractField(contract_type, id))
- .filter(({id}) => shouldShowByRelational(meta, id, data))
- .filter(({id, editable}) => shouldShowInMode(mode, id, editable))
- .filter(({id}) => fieldVisibleByConfig(fields_enable, id, contract_type, limit));
- return _.orderBy(fieldList,
- [({id}, idx) => {
- if ((fields).indexOf(id) > -1) {
- return (fields).indexOf(id);
- }
- return idx + 1000;
- }], ["asc"]);
- //TODO: move contract attributes up
- //TODO: order custom fileds by role based views
- }
- function getValue(value, data_type) { //value for fieldlist's components
- if (_.isNull(value)) {
- return "";
- } else if (isAutocomplete(data_type)) {
- if (_.isNull(value.id)) {
- return "";
- }
- return {
- value: value.id,
- label: value.label
- };
- } else if (data_type == "DATE") {
- return value ? moment.unix(value).toDate() : value;
- }
- return value;
- }
- const getSaveValue = (meta, id, value) => {
- let fieldMeta = _.get(meta, [id], {});
- if (isAutocomplete(fieldMeta.data_type)) {
- return _.has(value, "value") ? {id: value.value, label: value.label} : value;
- } else if (isCustomAutocomplete(fieldMeta.searchhelp_type)) {
- return _.get(value, "value", value);
- } else if (fieldMeta.data_type === "DATE") {
- if (!value) {
- return null;
- }
- return moment(value).unix();
- }
- return value;
- };
- const pickRelevantData = (meta, data) => {
- return _.pickBy(data, (value, id) => {
- return shouldShowByRelational(meta, id, data) && isRelevantContractField(data.contract_type, id);
- });
- };
- export {
- onAutocomplete,
- getValue,
- isAutocomplete,
- isCustomAutocomplete,
- getErrorsModel,
- formatBool,
- isEditable,
- getFieldList,
- getContractMeta,
- getContractDetails,
- getContractFromArchive,
- getContractFromHistory,
- restoreContract,
- isMandatory,
- isMandatoryEmpty,
- fieldNotVisible,
- getContractMedia,
- sendContractMedia,
- deleteContractMedia,
- setAsContractDisplayPhoto,
- getHistoryList,
- shouldShowByRelational,
- getInitialContractDetails,
- updateContract,
- createContract,
- wfTriggerCheck,
- isRelevantContractField,
- getSaveValue,
- isEmptyVal,
- pickRelevantData,
- hasChangesToSave,
- getFieldsConfiguredOnSB,
- getInitialVisibleFields,
- fieldVisibleByConfig
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement