"// This is the entry file for backend server service (for server-side validation and data conversion)

import {
  pdf,
  xml,
  flattenToStringMap,
  validateForm,
  getSignatureSummary,
  i18nByLocale,
  isEmpty,
  getI18nAddressLabel,
  getI18nDistrictLabel,
  getI18nRegionLabel,
} from "govhk-form-core";
import moment from "moment";
import get from "lodash/get";
import "../l10n";
import steps from "../steps";

const checkNull = (val) => {
  if (isEmpty(val) || val == "false") {return "No submission found"}
  else{ return val}
};

export default {
  convert: ({ form, submit, data }, dataTypes) => {
    const applicant = data.applicant;
    const sign = getSignatureSummary(form.locale, data, "applicant");
    const list = get(data["applicant"], "b", []);
    const mapping = {
      ...flattenToStringMap(data),
      sign,
      //a attachments
      newBuCertCopy: 
        get(data["applicant"], "newBuCertCopy.fileName", "")
      ,
      newBuAppFormCopy: 
        get(data["applicant"], "newBuAppFormCopy.fileName", "")
      ,
      incorCertCopy: 
        get(data["applicant"], "incorCertCopy.fileName", "")
      ,
      MACopy: get(data["applicant"], "MACopy.fileName", ""),
      a_Other: get(data["applicant"], "a_Other.fileName", ""),
      //b hkid
      "applicant.b.0.b_hkid": isEmpty(list[0])
        ? ""
        : `${list[0].b_hkid.id}(${list[0].b_hkid.checkDigit})`,
      "applicant.b.1.b_hkid": isEmpty(list[1])
        ? ""
        : `${list[1].b_hkid.id}(${list[1].b_hkid.checkDigit})`,
      "applicant.b.2.b_hkid": isEmpty(list[2])
        ? ""
        : `${list[2].b_hkid.id}(${list[2].b_hkid.checkDigit})`,
      // 'applicant.b.0.b_quaType.ee': isEmpty(list[0]) ? '' : `${list[0].b_quaType.ee}(${list[0].b_quaType.indexOf("ee")> -1 ? "Yes": ""}),
      //b attachments
      quahkidCopy: 
        get(data["applicant"], "quahkidCopy.fileName", "")
      ,
      quaSign: get(data["applicant"], "quaSign.fileName", ""),
      quaOther: get(data["applicant"], "quaOther.fileName", ""),
      //c hkid
      "applicant.c_hkid": `${applicant.c_hkid.id}(${applicant.c_hkid.checkDigit})`,
      //c attachments
      authHkid: get(data["applicant"], "authHkid.fileName", ""),
      authSign: get(data["applicant"], "authSign.fileName", ""),
      authBuRegRecord: 
        get(data["applicant"], "authBuRegRecord.fileName", "")
      ,
      authEmpConCopy: 
        get(data["applicant"], "authEmpConCopy.fileName", "")
      ,
      authMpfCopy: 
        get(data["applicant"], "authMpfCopy.fileName", "")
      ,
      authOther: get(data["applicant"], "authOther.fileName", "") ,
      //d hkid
      "applicant.d_hkid": `${applicant.d_hkid.id}(${applicant.d_hkid.checkDigit})`,
      //d attachments
      resHkid: get(data["applicant"], "resHkid.fileName", ""),
      resSign: get(data["applicant"], "resSign.fileName", ""),
      resBuRegRecord: 
        get(data["applicant"], "resBuRegRecord.fileName", "")
      ,
      resEmConCopy: 
        get(data["applicant"], "resEmConCopy.fileName", "")
      ,
      resMpfCopy: get(data["applicant"], "resMpfCopy.fileName", ""),
      resOther: get(data["applicant"], "resOther.fileName", ""),
      //e attachments
      manuAccLetterCopy: 
        get(data["applicant"], "manuAccLetterCopy.fileName", "")
      ,
      manuAppLetter: 
        get(data["applicant"], "manuAppLetter.fileName", "")
      ,
      //f attachments
      workPurCopy: 
        get(data["applicant"], "workPurCopy.fileName", "")
      ,
      workRecCopy: 
        get(data["applicant"], "workRecCopy.fileName", "")
      ,
      workAuthUse: 
        get(data["applicant"], "workAuthUse.fileName", "")
      ,
      workLayPlan: 
        get(data["applicant"], "workLayPlan.fileName", "")
      ,
      workUnderForm: 
        get(data["applicant"], "workUnderForm.fileName", "")
      ,
      workOther: get(data["applicant"], "workOther.fileName", ""),
      // date: moment(new Date()).format('YYYY-MM-DD'),
    };
    //b attachments
    if (list.length > 0) {
      for (let i = 0; i < list.length; i++) {
        mapping[`quaDegreeCertCopy_${i}`] = 
          !isEmpty(list[i].quaDegreeCertCopy) &&
            list[i].quaDegreeCertCopy.fileName
        ;
        mapping[`quaHdCertCopy_${i}`] = 
          !isEmpty(list[i].quaHdCertCopy) && list[i].quaHdCertCopy.fileName
        ;
        mapping[`quaPlumCertCopy_${i}`] = 
          !isEmpty(list[i].quaPlumCertCopy) && list[i].quaPlumCertCopy.fileName
        ;
        mapping[`quaBuRegRecord_${i}`] = 
          !isEmpty(list[i].quaBuRegRecord) && list[i].quaBuRegRecord.fileName
        ;
        mapping[`quaEmpConCopy_${i}`] = 
          !isEmpty(list[i].quaEmpConCopy) && list[i].quaEmpConCopy.fileName
        ;
        mapping[`quaMpfCopy_${i}`] = 
          !isEmpty(list[i].quaMpfCopy) && list[i].quaMpfCopy.fileName
        ;
      }
    }

    return [
      pdf.concat(
        [
          pdf.defaultAck(form, submit),
          pdf.merge({
            file: `${form.formId}-${form.locale}.pdf`,
            mapping,
          }),
        ],
        `${submit.trnId}_${submit.submitTime.replace(/[ :]/g, "-")}.pdf`
      ),
      xml.convert({ form, submit, data }, dataTypes, `${form.formId}.xml`),
    ];
  },
  validate: (submission, setResult) => {
    // console.log(JSON.stringify(submission.data));
    validateForm(submission, steps).then((result) => {
      setResult(result);
    });
  },
};
" can you help update it that if a_Other is empty then return ""