public async Task <Entity.Agreement> UpdateAgreement(Entity.Agreement updated) { if (updated.StartDate != null && updated.EndDate != null) { if (updated.StartDate > updated.EndDate) { return(null); } } var existingAgreement = await _context.Agreements .Where(a => a.AgreementId == updated.AgreementId) .Select(a => a) .FirstOrDefaultAsync(); if (existingAgreement == null) { await _context.AddAsync(updated); await _context.SaveChangesAsync(); return(updated); } else { existingAgreement.AgreementTemplateId = updated.AgreementTemplateId; if (updated.StartDate != null) { existingAgreement.StartDate = updated.StartDate; } if (updated.EndDate != null) { existingAgreement.EndDate = updated.EndDate; } if (updated.SignedDate != null) { existingAgreement.SignedDate = updated.SignedDate; } existingAgreement.TenantId = updated.TenantId; await _context.SaveChangesAsync(); return(await _context.Agreements .Where(a => a.AgreementId == updated.AgreementId) .Select(a => a) .FirstOrDefaultAsync()); } }
public async Task <IActionResult> UpdateAgreementTemplate(Entity.Agreement agreement) { if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin)) { var updatedAgreement = await _agreementRepository.UpdateAgreementTemplate(agreement); return(new ObjectResult(updatedAgreement)); } else { var err = new DTO.ErrorBuilder() .Message("You are not authorized to update agreement templates.") .Code(403) .Build(); return(err); } }
public async Task <Entity.Agreement> UpdateAgreementTemplate(Entity.Agreement agreement) { var existingAgreement = await _context.Agreements .Where(a => a.AgreementId == agreement.AgreementId) .Select(a => a) .FirstOrDefaultAsync(); if (existingAgreement == null) { await _context.AddAsync(agreement); } else { existingAgreement.Title = agreement.Title; existingAgreement.Text = agreement.Text; } await _context.SaveChangesAsync(); return(agreement); }
public async Task <IActionResult> UpdateAgreement(Entity.Agreement newInfo) { if (this.UserInRole(Role.Tenant)) { var userId = this.UserIdFromApiKey(); var tenantId = await _tenantRepository.TenantIdFromUserId(userId); if (tenantId == null) { var err = new DTO.ErrorBuilder() .Message("You are not a tenant of this property.") .Code(403) .Build(); _logger.LogWarning($"Attempt by user {userId} to access tenant information without being a tenant."); return(err); } var existingAgreement = await _agreementRepository.GetAgreement(newInfo.AgreementId); if (existingAgreement == null) { var err = new DTO.ErrorBuilder() .Message("Agreement not found.(1)") .Code(404) .Build(); return(err); } if (existingAgreement.TenantId != tenantId) { var err = new DTO.ErrorBuilder() .Message("Agreement not found.(2)") .Code(404) .Build(); return(err); } var updated = await _agreementRepository.UpdateAgreement(newInfo); return(new ObjectResult(updated)); } else if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin)) { var updated = await _agreementRepository.UpdateAgreement(newInfo); if (updated == null) { var err = new DTO.ErrorBuilder() .Message("An error occurred while updating the agreement.") .Code(400) .Build(); return(err); } return(new ObjectResult(updated)); } else { var err = new DTO.ErrorBuilder() .Message("You are not authorized to update agreements") .Code(403) .Build(); _logger.LogWarning($"Unauthorized access attempt to update agreements."); return(err); } }