private async Task SetLicenseSponsorAsUser(SofaLicense license, long sponsorDodId) { var sponsorLicense = await _licenseRepo.GetFirstOrDefault(l => l.DodId == sponsorDodId); //TODO Adding person to database would eliminate the need to add sponsor license first if (sponsorLicense == null) { throw new System.ArgumentException("Please add sponsor license first", string.Empty); } if (sponsorLicense.Dependents?.Count >= 14) { throw new ApplicationException("Max number of licenses added"); } license.SetSponsorId(sponsorLicense.Id); }
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); }