Пример #1
0
        public async Task <IActionResult> UploadInvoice(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.CreateInvoiceImportJobDefinition(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 Invoice table
            _backgroundJobClient.Enqueue(() => _service.ProcessInvoiceImportJob(jobDefinitionDTO.Id));
            return(Ok(jobDefinitionDTO));
        }
        public void ProcessInvoiceImportJob_JobId_ProcessEachLineOfInvoiceJobDetailsAndChangesItsStatusToProcessed()
        {
            //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\":\"77dc1029-b2ba-4beb-a8df-7214ae3baad8\",\"ProjectId\":\"00000000-0000-0000-0000-000000000000\",\"InvoiceNumber\":null,\"Total\":0.0,\"Paid\":false,\"Account\":null,\"Supplier\":\"HYPOVEREINSBANK BKK\",\"PurchaseOrderNumber\":null,\"InvoiceDate\":\"2019-05-06T00:00:00\",\"ReceivedOrCreatedDate\":\"0001 - 01 - 01T00: 00:00\",\"Status\":\"New\",\"LocalPaymentDate\":null}"
                    },
                    new CoupaImporterJobDefinitionDetail
                    {
                        Id               = new Guid("35246731-7870-400d-a646-456e5f824df7"),
                        IsProcessed      = false,
                        IsSuccessful     = null,
                        ErrorDescription = null,
                        LineNumber       = 2,
                        RawContent       = "{\"Id\":\"e7344d8f-809c-4667-bb87-ccf1d692eaa2\",\"ProjectId\":\"00000000-0000-0000-0000-000000000000\",\"InvoiceNumber\":null,\"Total\":0.0,\"Paid\":false,\"Account\":null,\"Supplier\":null,\"PurchaseOrderNumber\":null,\"InvoiceDate\":\"2019-05-06T00:00:00\",\"ReceivedOrCreatedDate\":\"0001 - 01 - 01T00: 00:00\",\"Status\":\"New\",\"LocalPaymentDate\":null}"
                    }
                }
            };

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

            //Act
            var response = _service.ProcessInvoiceImportJob(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));
        }