public async Task <SofaLicense> UpdateLicense(SofaLicense sofaLicense, long userId, bool isLicenseAuthenticatedinDB = false) { if (!await IsUserWriteAuthorized(sofaLicense, userId, isLicenseAuthenticatedinDB)) { throw new System.UnauthorizedAccessException("Unable to process update"); } sofaLicense.SetLastEditedById(userId); sofaLicense.SetDateUpdated(); if (!sofaLicense.IsValid()) { throw new System.ArgumentException("Invalid License Data", string.Empty); } await _licenseRepo.SaveAsync(); return(sofaLicense); }
public async Task <SofaLicense> SaveLicense(SofaLicense sofaLicense, long userId) { if ((await _licenseRepo.GetFirstOrDefault(l => l.DodId == sofaLicense.DodId)) != null) { throw new System.ArgumentException("DoD ID already exists in database", string.Empty); } sofaLicense.SetLastEditedById(userId); var user = await _userRepo.Get(userId); // Not Admin if (user.Account.AccountTypeId != (int)AccountTypes.Administrator) { // User if (user.Account.AccountTypeId == (int)AccountTypes.User) { if (userId != sofaLicense.DodId) { await SetLicenseSponsorAsUser(sofaLicense, userId); } else { sofaLicense.SetSponsorId(null); } } // CSS else if (user.Account.AccountTypeId == (int)AccountTypes.Css) { if (sofaLicense.UnitId != user.UnitId) { throw new ApplicationException("License must be from same unit as user"); } } sofaLicense.SetRemarks(null); sofaLicense.SetIsAuthenticated(false); sofaLicense.SetPermitNumber(GeneratePermitNumber()); } // Admin else { if (String.IsNullOrEmpty(sofaLicense.PermitNumber)) { sofaLicense.SetPermitNumber(GeneratePermitNumber()); } else { var indexOfDash = sofaLicense.PermitNumber.IndexOf("-"); int permitNumber; Int32.TryParse(sofaLicense.PermitNumber.Substring(indexOfDash + 1), out permitNumber); if (permitNumber == 0 || _licenseRepo.GetMaxPermitNumber() < permitNumber) { throw new System.ApplicationException("Invalid Permit Number"); } if (await _licenseRepo.GetFirstOrDefault(l => String.Equals(l.PermitNumber, sofaLicense.PermitNumber)) != null) { throw new System.ArgumentException("Permit Number Already Exists", string.Empty); } } } if (!sofaLicense.IsValid()) { throw new System.ArgumentException("Invalid License Data", string.Empty); } await _licenseRepo.Insert(sofaLicense); await _licenseRepo.SaveAsync(); return(sofaLicense); }