Exemple #1
0
        public string[] Validate(PublishTekArgs args)
        {
            if (args == null)
            {
                return(new[] { "Args is null." });
            }

            var errors = new List <string>();

            // The PubTEK key cannot be empty
            if (string.IsNullOrWhiteSpace(args.GGDKey))
            {
                return(new[] { "PubTEK key is null or empty." });
            }

            // The PubTEK key should be 6 or 7 characters in length
            if (args.GGDKey.Length != _luhnModNValidator.Config.ValueLength && args.GGDKey.Length != _luhnModNValidator.Config.ValueLength - 1)
            {
                return(new[] { "PubTEK key has incorrect length." });
            }

            // The PubTEK key should only have character from the given set
            if (args.GGDKey.Any(x => !_luhnModNValidator.Config.CharacterSet.Contains(x)))
            {
                return(new[] { "PubTEK key contains invalid character." });
            }

            // The PubTEK key should be validated for LuhnModN algorithm when it has 7 characters
            if (args.GGDKey.Length == _luhnModNValidator.Config.ValueLength && !_luhnModNValidator.Validate(args.GGDKey))
            {
                return(new[] { "PubTEK key validation for LuhnModN failed." });
            }

            //Should be a date only without time.
            args.SelectedDate = args.SelectedDate?.Date;
            if (!args.Symptomatic && (_dateTimeProvider.Snapshot.Date.AddDays(-30) > args.SelectedDate?.Date || args.SelectedDate?.Date > _dateTimeProvider.Snapshot.Date))
            {
                errors.Add($"Selected date out of range - {args.SelectedDate}.");
            }

            return(errors.ToArray());
        }
        /// <summary>
        /// Assume args validated
        /// </summary>
        /// <param name="args">The PubTEK values</param>
        /// <returns>True if succeeded, otherwise false</returns>
        public async Task <bool> ExecuteAsync(PublishTekArgs args)
        {
            // Defensive check for PublishTekArgs being null
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            // Retrieve the matching PubTEK value with all TEK's from the database
            var wf = await _workflowDb
                     .KeyReleaseWorkflowStates
                     .Include(x => x.Teks)
                     .FirstOrDefaultAsync(x => x.GGDKey == args.GGDKey || args.GGDKey.StartsWith(x.LabConfirmationId));

            // If no PubTEK value is found the process should be ended. The PubTEK key does not exist or is already processed/published.
            if (wf == null)
            {
                _logger.WriteKeyReleaseWorkflowStateNotFound(args.GGDKey);
                return(false);
            }

            wf.AuthorisedByCaregiver = _dateTimeProvider.Snapshot;
            wf.LabConfirmationId     = null;                //Clear from usable key range
            wf.GGDKey = null;                               //Clear from usable key range
            wf.StartDateOfTekInclusion = args.SelectedDate; // The date is currently a StartDateOfTekInclusion or Date of Test. The system lacks having 2 date variants so the existing StartDateOfTekInclusion will hold either
            wf.IsSymptomatic           = args.Symptomatic ? InfectiousPeriodType.Symptomatic : InfectiousPeriodType.Asymptomatic;

            var success = await PublishTek(wf);

            if (success)
            {
                _logger.LogInformation($"GGDKey {wf.GGDKey} authorized.");
            }

            return(success);
        }