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 CopyCentralPlanCommand(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_SavesCentralReport(ReportingFrequency reportingFrequency, ReportingFrequency differentFrequency) { 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.Central) .SetArgument(o => o.ReportingFrequency, reportingFrequency) .BuildAndPersist(s); var existing = new CentralReportBuilder() .SetDescription(DataProvider.Get <string>()) .SetOrganization(organization) .SetReportingPeriod(new ReportingPeriod(reportingFrequency, reportingTerm, year)) .BuildAndPersist(s); var newReportYear = year + 1; new CentralReportBuilder() .SetDescription(DataProvider.Get <string>()) .SetOrganization(organization) .SetReportingPeriod(new ReportingPeriod(differentFrequency, reportingTerm, newReportYear)) .BuildAndPersist(s); var cmd = new CopyCentralPlanCommand(existing.Id, organization, newReportYear, reportingTerm, reportingFrequency); var description = cmd.Description; var lastPeriodData = Calculator.GetLastPeriodUpdateData(existing); var expected = new CentralReportBuilder() .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 <CopyCentralPlanCommandHandler>(AssemblySetupFixture.EndpointTestContainer, (h, ctx) => { ctx.SetUsernameOnHeader(testParams.username); return(h.Handle(testParams.cmd, ctx)); }); var evt = context.ExpectPublish <ICentralPlanCopied>(); Endpoint.AssertOnSqlSessionThat(AssemblySetupFixture.EndpointTestContainer, s => { var report = s.Query <CentralReport>().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 <ICentralPlanCopied>(e => { e.Organization = testParams.expected.Organization; e.Username = testParams.username; e.CentralReport = reportRef; }); evt.Should().BeEquivalentTo(expectedEvt, e => e.Excluding(p => p.SerializedData)); evt.SerializedData.Should().NotBe(null); }); }