public async Task <AgreementResult> CreateAgreement(IMarketingAgencyOwner marketingAgencyOwner, Guid providerOrganizationId) { var retVal = new AgreementResult() { ProviderOrganizationId = providerOrganizationId, MarketingOrganizationId = marketingAgencyOwner.OrganizationId }; var marketingOrganization = await _marketingOrganizations.Queryable() .Where(x => x.Id == marketingAgencyOwner.OrganizationId) .FirstOrDefaultAsync(); if (marketingOrganization == null) { retVal.ErrorMessage = "Organization is not configured correctly"; return(retVal); } _logger.LogDebug(GetLogMessage("Marketing Organization Found: {0}"), marketingOrganization.Id); var providerOrganization = await _providerOrganization.Queryable() .Where(x => x.Id == providerOrganizationId) .FirstOrDefaultAsync(); if (providerOrganization == null) { throw new ApplicationException("Provider organization was not found"); } _logger.LogDebug(GetLogMessage("Provider Organization Found: {0}"), providerOrganization.Id); var agreement = new MarketingAgreement { MarketingStream = marketingOrganization.MarketerStream, MarketingBonus = marketingOrganization.MarketerBonus, MarketingAgencyBonus = marketingOrganization.MarketingAgencyBonus, MarketingAgencyStream = marketingOrganization.MarketingAgencyStream, MarketingOrganizationId = marketingAgencyOwner.OrganizationId, ProviderOrganizationId = providerOrganizationId, Status = AgreementStatus.AwaitingApproval, ObjectState = ObjectState.Added, InitiatedByProvider = false, Created = DateTimeOffset.UtcNow }; _logger.LogDebug(GetLogMessage("Marketing Agreement: {@agreement}"), agreement); var result = Repository.InsertOrUpdateGraph(agreement, true); _logger.LogDebug(GetLogMessage("{0} Records updated in database"), result); return(await Task.FromResult(new AgreementResult() { Succeeded = result > 0, MarketingOrganizationId = agreement.MarketingOrganizationId, ProviderOrganizationId = agreement.ProviderOrganizationId })); }
public Task <AgreementResult> CreateAgreement(IProviderAgencyOwner providerAgencyOwner, Guid marketingOrganizationId) { _logger.LogInformation(GetLogMessage("Creating agreement between provider {0} and marketer {1}"), providerAgencyOwner.OrganizationId, marketingOrganizationId); var marketingOrganization = UnitOfWork .RepositoryAsync <MarketingOrganization>().Queryable() .Include(x => x.MarketingAgreements) .FirstOrDefault(x => x.Id == marketingOrganizationId); if (marketingOrganization == null) { throw new ApplicationException("Marketing organization not found"); } _logger.LogDebug(GetLogMessage("Marketing organization found: {0}"), marketingOrganization.Id); if (marketingOrganization.Discoverable == false) { throw new ApplicationException("Marketing organization is not accepting offers"); } if (marketingOrganization.MarketingAgreements.Any(x => x.ProviderOrganizationId == providerAgencyOwner.OrganizationId)) { throw new ApplicationException("An existing offer already exists"); } var agreement = new MarketingAgreement() { ProviderOrganizationId = providerAgencyOwner.OrganizationId, MarketingOrganizationId = marketingOrganizationId, Status = AgreementStatus.AwaitingApproval, ObjectState = ObjectState.Added, InitiatedByProvider = true, MarketingAgencyStream = marketingOrganization.MarketingAgencyStream, MarketerBonus = marketingOrganization.MarketerBonus, MarketingAgencyBonus = marketingOrganization.MarketingAgencyBonus, MarketerStream = marketingOrganization.MarketerStream }; var result = Repository.InsertOrUpdateGraph(agreement, true); _logger.LogDebug(GetLogMessage("{0} records updated in database"), result); if (result > 0) { Task.Run(() => RaiseEvent(new MarketingAgreementCreated() { ProviderOganizationId = providerAgencyOwner.OrganizationId, MarketingOrganizationId = marketingOrganizationId })); } return(Task.FromResult(new AgreementResult() { Succeeded = result > 0, MarketingOrganizationId = agreement.MarketingOrganizationId, ProviderOrganizationId = agreement.ProviderOrganizationId })); }