コード例 #1
0
        public async Task <IHttpActionResult> CopyCentralPlan(
            int copyFromReportId,
            OrganizationReference organization,
            int year,
            ReportingTerm reportingTerm,
            ReportingFrequency reportingFrequency)
        {
            try
            {
                if (!_userContext.CurrentUserIsSystemUser())
                {
                    return(Unauthorized());
                }

                if (organization == null)
                {
                    throw new ArgumentNullException(nameof(organization));
                }

                if (!_userContext.CurrentUserCanAccess(organization.Id))
                {
                    return(Unauthorized());
                }

                var cmd = new CopyUnitPlanCommand(copyFromReportId, organization, year, reportingTerm, reportingFrequency);
                await _endpointInstance().SendWithSignalRMetaData(cmd, Request);

                return(this.Accepted());
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                return(InternalServerError());
            }
        }
        public async Task Handle_SavesUnitReport(ReportingFrequency reportingFrequency)
        {
            DateTimeDbTestExtensions.SetUtcNowToRandomDate();
            var testParams = Endpoint.ArrangeOnSqlSession(AssemblySetupFixture.EndpointTestContainer,
                                                          s =>
            {
                var username      = DataProvider.Get <string>();
                var year          = 2019;
                var reportingTerm = ReportingTerm.One;
                var organization  = new TestObjectBuilder <Organization>()
                                    .SetArgument(o => o.OrganizationType, OrganizationType.Unit)
                                    .SetArgument(o => o.ReportingFrequency, reportingFrequency)
                                    .BuildAndPersist(s);

                var existing = new UnitReportBuilder()
                               .SetDescription(DataProvider.Get <string>())
                               .SetOrganization(organization)
                               .SetReportingPeriod(new ReportingPeriod(reportingFrequency, reportingTerm, year))
                               .BuildAndPersist(s);
                var newReportYear = year + 1;
                var cmd           = new CopyUnitPlanCommand(existing.Id, organization, newReportYear, reportingTerm);
                var description   = cmd.Description;

                var lastPeriodData = Calculator.GetLastPeriodUpdateData(existing);

                var expected = new UnitReportBuilder()
                               .SetDescription(description)
                               .SetOrganization(organization)
                               .SetReportingPeriod(new ReportingPeriod(reportingFrequency, reportingTerm, newReportYear))
                               .SetReportData(existing)

                               .Build();

                expected.Update(lastPeriodData);

                return(new
                {
                    cmd,
                    username,
                    expected
                });
            });

            var context = await Endpoint.Act <CopyUnitPlanCommandHandler>(AssemblySetupFixture.EndpointTestContainer,
                                                                          (h, ctx) =>
            {
                ctx.SetUsernameOnHeader(testParams.username);
                return(h.Handle(testParams.cmd, ctx));
            });

            var evt = context.ExpectPublish <IUnitPlanCopied>();

            Endpoint.AssertOnSqlSessionThat(AssemblySetupFixture.EndpointTestContainer,
                                            s =>
            {
                var report = s.Query <UnitReport>().OrderByDescending(o => o.Id).First();
                report.Should().NotBeNull();
                report.Should().BeEquivalentTo(testParams.expected, e =>
                                               e.Excluding(p => p.Id)
                                               .Excluding(p => p.ReportingPeriod)
                                               .Excluding(p => p.ReportStatus));

                EntityReference reportRef = report;

                var expectedEvt = Test.CreateInstance <IUnitPlanCopied>(e =>
                {
                    e.Organization = testParams.expected.Organization;
                    e.Username     = testParams.username;
                    e.UnitReport   = reportRef;
                });

                evt.Should().BeEquivalentTo(expectedEvt, e => e.Excluding(p => p.SerializedData));
                evt.SerializedData.Should().NotBe(null);
            });
        }