private async Task SetSponsorId(LicenseDTO license)
        {
            if (license.SponsorDodId == null)
            {
                license.SponsorId = null;
                return;
            }

            var sponsor = await _licenseRepo.GetFirstOrDefault(l => l.DodId == license.SponsorDodId);

            if (sponsor == null)
            {
                throw new System.ArgumentException("Sponsor license not found");
            }
            else if (sponsor.UnitId != license.UnitId)
            {
                throw new System.ApplicationException("Dependent must be from same unit as sponsor");
            }

            license.SponsorId = sponsor.Id;
        }
Example #2
0
        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);
        }