public async Task GetDocumentDefinitionByType()

in packages/ekyc-api/src/ekyc-api/Utils/DocumentDefinitionFactory.cs [56:131]


        public async Task<DocumentDefinitionBase> GetDocumentDefinitionByType(DocumentTypes documentType)
        {
            var jsonPath = "./DocumentDefinitions/documentdefinitions.json";

            if (!File.Exists(jsonPath))
                throw new Exception("Document definition Json file not found.");

            string strJson;

            using (var sr = File.OpenText(jsonPath))
            {
                strJson = await sr.ReadToEndAsync();
            }

            var definitions = JsonSerializer.Deserialize<DocumentDefinitionsContainer>(strJson);

            if (definitions == null)
                throw new Exception("Error deserializing document definitions.");

            List<DocumentDefinitionBase> definitionInstances = new List<DocumentDefinitionBase>();

            foreach (System.Type T in
                Assembly.GetAssembly(typeof(DocumentDefinitionBase)).GetTypes()
                    .Where(myType =>
                        myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(DocumentDefinitionBase))))
            {
                var dd = (DocumentDefinitionBase)_serviceProvider.GetService(T);
                definitionInstances.Add(dd);
            }

            var documentDefinition = definitionInstances.FirstOrDefault(a => a.DocumentType == documentType);

            if (documentDefinition == null)
                throw new Exception($"Document type {documentType} is not supported.");

            /*  DocumentDefinitionBase documentDefinition;
  
              switch (documentType)
              {
                  case DocumentTypes.ID_KTP:
                      documentDefinition =
                          (DocumentDefinitionBase)_serviceProvider.GetService(typeof(ID_KTP_DocumentDefinition));
                      break;
                  case DocumentTypes.MY_NRIC:
                      documentDefinition =
                          (DocumentDefinitionBase)_serviceProvider.GetService(typeof(MY_NRIC_DocumentDefinition));
                      break;
                  case DocumentTypes.AU_PASSPORT:
                      documentDefinition =
                          (DocumentDefinitionBase)_serviceProvider.GetService(typeof(AU_Passport_DocumentDefinition));
                      break;
                  case DocumentTypes.KH_IC:
                      documentDefinition =
                          (DocumentDefinitionBase)_serviceProvider.GetService(typeof(KH_IC_DocumentDefinition));
                      break;
                  case DocumentTypes.PRC_PASSPORT:
                      documentDefinition =
                          (DocumentDefinitionBase)_serviceProvider.GetService(typeof(PRC_Passport_DocumentDefinition));
                      break;
                  default:
                      throw new Exception($"Document type {documentType} is not supported.");
              }
  */
            var documentTypeDTO = definitions.DocumentTypes
                .FirstOrDefault(a =>
                    string.Equals(a.Name, documentType.ToString(), StringComparison.CurrentCultureIgnoreCase));

            documentDefinition.Landmarks = documentTypeDTO.Landmarks;
            documentDefinition.Name = documentTypeDTO.Name;
            documentDefinition.DataFields = documentTypeDTO.DataFields;
            documentDefinition.LivenessSupported = documentTypeDTO.LivenessSupported;
            documentDefinition.FaceExtractionSupported = documentTypeDTO.FaceExtractionSupported;
            documentDefinition.SignatureExtractionSupported = documentTypeDTO.SignatureExtractionSupported;

            return documentDefinition;
        }