Ejemplo n.º 1
0
        public async Task <IActionResult> UploadRequisition(Guid projectId)
        {
            if (Request.Form.Files.Count == 0)
            {
                return(new UnsupportedMediaTypeResult());
            }

            var file = Request.Form.Files[0];

            if (!file.ContentType.Contains("text/csv") && !file.ContentType.Contains("ms-excel"))
            {
                return(new UnsupportedMediaTypeResult());
            }

            if (file.Length == 0)
            {
                return(new UnsupportedMediaTypeResult());
            }

            CoupaImporterJobDefinitionDTO jobDefinitionDTO;

            using (var memoryStream = new MemoryStream())
            {
                await file.CopyToAsync(memoryStream);

                jobDefinitionDTO = _service.CreateRequisitionImportJobDefinition(projectId, memoryStream, file.FileName);
                if (jobDefinitionDTO == null)
                {
                    return(BadRequest(
                               "An error occurred while uploading the file, please try again or contact the system administrator."));
                }
            }
            await _unitOfWork.SaveChangesAsync();

            // Enqueue background job to import all data in Requisition table
            _backgroundJobClient.Enqueue(() => _service.ProcessRequisitionImportJob(jobDefinitionDTO.Id));
            return(Ok(jobDefinitionDTO));
        }
        public void ProcessRequisitionImportJob_JobId_ProcessEachLineOfPurchaseOrderJobDetailsAndChangesItsStatusToProcessed()
        {
            //Arrange
            var jobDefinitionId = new Guid("45744a2e-3dc1-472f-9bb5-378a0a9eeda4");

            var jobDefinition = new CoupaImporterJobDefinition
            {
                Id       = jobDefinitionId,
                FileName = "Test.csv",
                Status   = (int)CoupaImporterStatus.Pending,
                CoupaImporterJobDefinitionDetails = new List <CoupaImporterJobDefinitionDetail>
                {
                    new CoupaImporterJobDefinitionDetail
                    {
                        Id               = new Guid("90d18a32-5090-4091-9c16-91dcb7983c38"),
                        IsProcessed      = false,
                        IsSuccessful     = null,
                        ErrorDescription = null,
                        LineNumber       = 1,
                        RawContent       = "{\"Id\":\"54a85421-ab58-4fef-ae4c-ab5651501291\",\"ProjectId\":\"00000000-0000-0000-0000-000000000000\",\"PurchaseOrderNumber\":7116,\"OrderTotal\":7630.0,\"AccountingTotal\":5608.64,\"Account\":\"6063-521-5412424-203050-SN-00000-000-000\",\"Supplier\":\"MEDIALINK PRINTING SERVICES PTE LTD\",\"OrderDate\":\"2019-05-05T00:00:00\",\"Item\":\"Q2 printing of brochures and placemats for SG Banks\",\"AccountingTotalCurrency\":\"USD\",\"Currency\":\"SGD\",\"ShippingAddress\":\"Twenty Anson, 20 Anson Road\n#18-01 Singapore 079912 Singapore\", \"ProjectDescription\":null, \"Commodity\":\"Mktg Print & Fulfillment (5412424 Print - Marketing Collateral)\", \"CostCode\":null, \"FixedAsset_CIPCategory\":null, \"TargetLocationCode\":null, \"ShipTo\":\"Andy Lim\", \"RequestedBy\":\"Andy Lim\", \"CreatedBy\":\"Doris Ma\", \"LastUpdatedBy\":\"Doris Ma\"}"
                    },
                    new CoupaImporterJobDefinitionDetail
                    {
                        Id               = new Guid("35246731-7870-400d-a646-456e5f824df7"),
                        IsProcessed      = false,
                        IsSuccessful     = null,
                        ErrorDescription = null,
                        LineNumber       = 2,
                        RawContent       = "{\"Id\":\"97647256-c8f1-4065-babb-670e7943a04b\",\"ProjectId\":\"00000000-0000-0000-0000-000000000000\",\"PurchaseOrderNumber\":7117,\"OrderTotal\":16100.0,\"AccountingTotal\":2052.22,\"Account\":\"6063-521-5412424-203050-SN-00000-000-000\",\"Supplier\":\"HETERMEDIA SERVICES LIMITED\",\"OrderDate\":\"2019-05-05T00:00:00\",\"Item\":\"Quarterly Update of Marketing Collaterals - Q2\",\"AccountingTotalCurrency\":\"USD\",\"Currency\":\"HKD\",\"ShippingAddress\":\"Twenty Anson, 20 Anson Road\\n#18-01\nSingapore 079912\nSingapore\",\"ProjectDescription\":null,\"Commodity\":\"Mktg Print & Fulfillment (5412424 Print - Marketing Collateral)\",\"CostCode\":null,\"FixedAsset_CIPCategory\":null,\"TargetLocationCode\":null,\"ShipTo\":\"Andy Lim\",\"RequestedBy\":\"Andy Lim\",\"CreatedBy\":\"Karen Leung\",\"LastUpdatedBy\":\"Karen Leung\"}"
                    }
                }
            };

            _coupaImporterRepository.Get(jobDefinitionId)
            .Returns(jobDefinition);

            //Act
            var response = _service.ProcessRequisitionImportJob(jobDefinitionId);

            //Assert
            Assert.AreEqual(CoupaImporterStatus.Processed, response.Status);
            Assert.IsEmpty(response.CoupaImporterJobDefinitionDetails.Where(d => d.IsProcessed == false));

            _coupaImporterRepository.Received(1)
            .UpdateAllJobDefinitionDetail(
                jobDefinition.Id,
                Arg.Is <List <CoupaImporterJobDefinitionDetail> >(d =>
                                                                  d.First().Id == jobDefinition.CoupaImporterJobDefinitionDetails.First().Id&&
                                                                  d.First().IsSuccessful == true && d.First().IsProcessed == true));

            _coupaImporterRepository.Received(1)
            .UpdateAllJobDefinitionDetail(
                jobDefinition.Id,
                Arg.Is <List <CoupaImporterJobDefinitionDetail> >(d =>
                                                                  d.First().Id == jobDefinition.CoupaImporterJobDefinitionDetails.First().Id&&
                                                                  d.Last().IsSuccessful == true && d.Last().IsProcessed == true));

            _coupaImporterRepository.Received(1)
            .Update(jobDefinitionId,
                    Arg.Is <CoupaImporterJobDefinition>(j =>
                                                        j.Id == jobDefinitionId && j.Status == (int)CoupaImporterStatus.Processed));
        }