根据另一个嵌套对象删除和添加属性

时间:2019-07-05 10:16:14

标签: javascript

我有两个嵌套对象,其中一个对象决定删除或保留另一个对象 属性。因此,基于userAccessData对象,我正在对allData执行此操作 我还有一个条件,即如果字段有操作具有读取访问权限,则字段 所需的值将更新为false。

我已经做了以上事情

请参见下面的代码,我已经实现了。但是有些东西像 如果fields数组为空,则表单本身应删除。也是fieldRegexRules 属性应该删除字段,并且我需要以与allData相同的顺序获取它,以及我如何传递输入。我也添加了预期的输出。

任何帮助

const userAccessData = {
  forms: {
    student: [
      {
        studentDetails: ['read', 'create', 'update'],
      },
    ],
    class: [
      {
        classDetails: ['read', 'create', 'update'],
      },
      {
        classSecondaryDetails: ['read', 'create', 'update'],
      },
    ],
    school: [
      {
        schoolContact: ['read', 'create', 'update'],
      },
      {
        schoolAddress: ['read'],
      },
      {
        schoolBasicDetails: ['read', 'create'],
      },
      {
        schoolLocationDetails: ['read', 'create', 'update'],
      },
    ],
  },
  fields: {
    school: {
      schoolAddress: [
        {
          isAddress: ['read', 'update'],
        },
      ],
      schoolLocationDetails: [
        {
          status: ['read'],
        },
      ],
      schoolContact: [
        {
          contactAddress: ['read'],
        },
        {
          contactStatus: ['read', 'create'],
        },
      ],
    },
    student: {
      studentDetails: [
        {
          isAvailable: ['read'],
        },
      ],
    },
    class: {
      classDetails: [
        {
          classId: ['read', 'create', 'update'],
        },
      ],
    },
  },
};




const allData = {
  forms: [
    {
      key: 'schoolBasicDetails',
      fields: [
        {
          required: false,
          key: 'numberId',
        },
        {
          required: false,
          key: 'regId',
        },
        {
          required: true,
          key: 'gstinId',
        },
      ],
    },
    {
      key: 'schoolLocationDetails',
      fields: [
        {
          required: true,
          key: 'status',
        },
        {
          required: false,
          key: 'areaNumber',
        },
      ],
    },
    {
      key: 'schoolAddress',
      fields: [
        {
          required: true,
          key: 'address',
        },
      ],
    },
    {
      key: 'schoolContact',
      fields: [
        {
          required: false,
          key: 'contactAddress',
        },
        {
          required: false,
          key: 'contactStatus',
        },
      ],
    },
    {
      key: 'schoolContactAdditional',
      fields: [
        {
          required: false,
          key: 'contactAddressNew',
        },
        {
          required: true,
          key: 'contactStatusNew',
        },
      ],
    },
  ],
  fieldRegexRules: {
    contactAddress: {
      regexRule: '^\\d{10}$',
      required: false,
    },
    contactStatus: {
      regexRule: '^[\\d.]+$',
      required: false,
    },
    contactAddressNew: {
      regexRule: '^\\d{10}$',
      required: false,
    },
    contactStatusNew: {
      regexRule: '^[\\d.]+$',
      required: true,
    },
    address: {
      regexRule: '^[\\d.]+$',
      required: true,
    },
    status: {
      regexRule: '^[a-zA-Z0-9-,!)+?(_ ]{1,250}$',
      required: true,
    },
    areaNumber: {
      regexRule: '',
      required: true,
    },
    numberId: {
      regexRule: '',
      required: false,
    },
    regId: {
      regexRule: '',
      required: true,
    },
    gstinId: {
      regexRule: '',
      required: true,
    },
  },
  fieldsLevelIndex: {
    key: [1, 2, 3],
    data: ['one', 'two', 'three']
  },
}; 


const givingAccessForForms = (
  accessList,
  formData,
  selectedSection,
) => {
  const filteredAttrGrps = accessList[selectedSection].map((formAccess) => {
    const formHasAccess = Object.keys(formAccess)[0];
    const filteredForm = formData.forms.find(
      form => form.key === formHasAccess,
    );
    const accessListOfForm = accessList[selectedSection].find(o => Object.keys(o)[0] === formHasAccess)
    const actions = accessListOfForm[formHasAccess];
    return {
      ...filteredForm,
      access: actions,
    };
  });
  return {
    ...formData,
    forms: filteredAttrGrps,
  };
};

const givingAccessForFields = (formDataWithAccess, accessList, selectedSection) => {
  const formsOfSelectedSection = Object.keys(accessList[selectedSection]);
  const filteredFormWithFieldAcccess = formDataWithAccess.forms.map((form) => {
    const filterFieldsWithAccess = form.fields.filter((field) => {
      if (formsOfSelectedSection.indexOf(form.key) > -1) {
        const findedField = accessList[selectedSection][form.key].find(
          findAttr => Object.keys(findAttr)[0] === field.key,
        );
        if (findedField) {
          if (
            findedField[field.key].indexOf('read') > -1 &&
            (
              findedField[field.key].indexOf('create') === -1 &&
              findedField[field.key].indexOf('update') === -1
            )
          ) {
            formDataWithAccess.fieldRegexRules[
              field.key
            ].required = false;
            field.required = false;
          }
          field.action = findedField[field.key];
          return field;
        }
      }
    });
    return {
      ...form,
      fields: filterFieldsWithAccess,
    };
  });
  return {
    ...formDataWithAccess,
    forms: filteredFormWithFieldAcccess,
  };
};


let withFormLevelAccess = givingAccessForForms(userAccessData.forms, allData, 'school')
let withFieldLevelAccess = givingAccessForFields(withFormLevelAccess, userAccessData.fields, 'school')

console.log(withFieldLevelAccess)

预期输出

{
  "forms": [
    {
      "key": "schoolContact",
      "fields": [
        {
          "required": false,
          "key": "contactAddress",
          "action": [
            "read"
          ]
        },
        {
          "required": false,
          "key": "contactStatus",
          "action": [
            "read",
            "create"
          ]
        }
      ],
      "access": [
        "read",
        "create",
        "update"
      ]
    },
    {
      "key": "schoolLocationDetails",
      "fields": [
        {
          "required": false,
          "key": "status",
          "action": [
            "read"
          ]
        }
      ],
      "access": [
        "read",
        "create",
        "update"
      ]
    }
  ],
  "fieldRegexRules": {
    "contactAddress": {
      "regexRule": "^\\d{10}$",
      "required": false
    },
    "contactStatus": {
      "regexRule": "^[\\d.]+$",
      "required": false
    },
    "status": {
      "regexRule": "^[a-zA-Z0-9-,!)+?(_ ]{1,250}$",
      "required": false
    },
  },
  "fieldsLevelIndex": {
    "key": [
      1,
      2,
      3
    ],
    "data": [
      "one",
      "two",
      "three"
    ]
  }
}

0 个答案:

没有答案