Ejemplo n.º 1
0
    private bool TryProcessDuplicates(SlimAccommodationData matchedAccommodation, Contracts.MultilingualAccommodation accommodation)
    {
        if (_context.ChangeTracker.Entries <RichAccommodationDetails>()
            .Any(ac => ac.Entity.Id == matchedAccommodation.HtId))
        {
            var entry = _context.ChangeTracker.Entries <RichAccommodationDetails>()
                        .Single(ac => ac.Entity.Id == matchedAccommodation.HtId);

            _logger.LogSameAccommodationInOneSupplierError(_supplier.ToString(), entry.Entity.SupplierAccommodationCodes[_supplier],
                                                           accommodation.SupplierCode);

            if (!_accommodationDataStorage.SupplierActiveAccommodations.TryGetValue(accommodation.SupplierCode, out var duplicate))
            {
                ProcessDeactivation(accommodation.SupplierCode, DeactivationReasons.DuplicateInOneSupplier, accommodation);
                return(true);
            }

            _accommodationChangeLogCollectorService.CollectDuplicateAccommodationSupplierCodesMergeEvent(matchedAccommodation.HtId,
                                                                                                         AccommodationChangeEventSources.Updater, duplicate.HtId, matchedAccommodation.SupplierAccommodationCodes,
                                                                                                         duplicate.SupplierAccommodationCodes);

            var duplicateCodes = new Dictionary <string, string>();
            foreach (var supplierCode in duplicate.SupplierAccommodationCodes)
            {
                if (!entry.Entity.SupplierAccommodationCodes.TryAdd(supplierCode.Key, supplierCode.Value))
                {
                    duplicateCodes.Add(supplierCode.Key, supplierCode.Value);
                }
            }

            entry.Entity.IsCorrected = false;
            entry.Property(p => p.SupplierAccommodationCodes).IsModified = true;
            entry.Property(p => p.IsCorrected).IsModified = true;

            DeactivateDuplicate(duplicate, duplicateCodes);
            return(true);
        }

        if (matchedAccommodation.SupplierAccommodationCodes.ContainsKey(_supplier))
        {
            _logger.LogSameAccommodationInOneSupplierError(_supplier, accommodation.SupplierCode,
                                                           matchedAccommodation.SupplierAccommodationCodes[_supplier]);
            if (!_accommodationDataStorage.SupplierActiveAccommodations.TryGetValue(accommodation.SupplierCode, out var duplicate))
            {
                ProcessDeactivation(accommodation.SupplierCode, DeactivationReasons.DuplicateInOneSupplier, accommodation);
                return(true);
            }

            _accommodationChangeLogCollectorService.CollectDuplicateAccommodationSupplierCodesMergeEvent(matchedAccommodation.HtId,
                                                                                                         AccommodationChangeEventSources.Updater, duplicate.HtId, matchedAccommodation.SupplierAccommodationCodes,
                                                                                                         duplicate.SupplierAccommodationCodes);

            var accommodationToUpdate = new RichAccommodationDetails
            {
                Id          = matchedAccommodation.HtId,
                Modified    = DateTimeOffset.UtcNow,
                IsCorrected = false,
                SupplierAccommodationCodes = matchedAccommodation.SupplierAccommodationCodes
            };

            var duplicateCodes = new Dictionary <string, string>();

            foreach (var supplierCode in duplicate.SupplierAccommodationCodes)
            {
                if (!accommodationToUpdate.SupplierAccommodationCodes.TryAdd(supplierCode.Key, supplierCode.Value))
                {
                    duplicateCodes.Add(supplierCode.Key, supplierCode.Value);
                }
            }

            _context.Attach(accommodationToUpdate);
            var firstEntry = _context.Entry(accommodationToUpdate);
            firstEntry.Property(e => e.Modified).IsModified    = true;
            firstEntry.Property(e => e.IsCorrected).IsModified = true;
            firstEntry.Property(e => e.SupplierAccommodationCodes).IsModified = true;

            DeactivateDuplicate(duplicate, duplicateCodes);

            return(true);
        }

        return(false);


        void DeactivateDuplicate(SlimAccommodationData duplicate, Dictionary <string, string> duplicateCodes)
        {
            var dbDuplicate = new RichAccommodationDetails
            {
                Id       = duplicate.HtId,
                Modified = DateTimeOffset.UtcNow,
                SupplierAccommodationCodes = duplicateCodes,
                DeactivationReason         = DeactivationReasons.DuplicateInOneSupplier,
                IsActive = false
            };

            _context.Attach(dbDuplicate);
            var secondEntry = _context.Entry(dbDuplicate);

            secondEntry.Property(e => e.Modified).IsModified = true;
            secondEntry.Property(e => e.SupplierAccommodationCodes).IsModified = true;
            secondEntry.Property(e => e.IsActive).IsModified           = true;
            secondEntry.Property(e => e.DeactivationReason).IsModified = true;

            _accommodationsToRemove.Add(duplicate.HtId);

            _accommodationChangeLogCollectorService.CollectDuplicateAccommodationCommonSuppliersCodesRemoveEvent(duplicate.HtId,
                                                                                                                 AccommodationChangeEventSources.Updater, matchedAccommodation.HtId, duplicate.SupplierAccommodationCodes,
                                                                                                                 matchedAccommodation.SupplierAccommodationCodes);

            AddOrUpdateHtAccommodationMappings(duplicate.HtId, matchedAccommodation.HtId);
        }
    }
Ejemplo n.º 2
0
    public void Update(Contracts.MultilingualAccommodation accommodation, SlimAccommodationData matchedAccommodation)
    {
        // This is temporary solution
        if (TryProcessDuplicates(matchedAccommodation, accommodation))
        {
            return;
        }

        var accommodationToUpdate = new RichAccommodationDetails
        {
            Id          = matchedAccommodation.HtId,
            Modified    = DateTimeOffset.UtcNow,
            IsCorrected = false,
            SupplierAccommodationCodes = matchedAccommodation.SupplierAccommodationCodes
        };

        _context.Accommodations.Attach(accommodationToUpdate);
        _context.Entry(accommodationToUpdate).Property(p => p.IsCorrected).IsModified = true;
        _context.Entry(accommodationToUpdate).Property(p => p.Modified).IsModified    = true;

        if (_accommodationDataStorage.SupplierActiveAccommodations.TryGetValue(accommodation.SupplierCode, out var existingAccommodation))
        {
            Merge(existingAccommodation);
        }

        else if (_accommodationDataStorage.SupplierInvalidAccommodations.TryGetValue(accommodation.SupplierCode, out var existingInvalidAccommodation))
        {
            Merge(existingInvalidAccommodation);
        }
        else
        {
            _accommodationChangeLogCollectorService.CollectNewSupplierAdditionEvent(accommodationToUpdate.Id, AccommodationChangeEventSources.Updater,
                                                                                    matchedAccommodation.SupplierAccommodationCodes, _supplier, accommodation.SupplierCode);

            accommodationToUpdate.SupplierAccommodationCodes.Add(_supplier, accommodation.SupplierCode);
        }

        _context.Entry(accommodationToUpdate).Property(p => p.SupplierAccommodationCodes).IsModified = true;


        void FindAndDeactivateUncertainMatch(int firstHtId, int secondHtId)
        {
            var existingUncertainMatch = _accommodationDataStorage.ActiveUncertainMatches
                                         .FirstOrDefault(eum => eum.Value.Equals(new(firstHtId, secondHtId)) ||
                                                         eum.Value.Equals(new(secondHtId, firstHtId)));

            if (existingUncertainMatch.Key != default)
            {
                var uncertainMatchToDeactivate = new AccommodationUncertainMatches
                {
                    Id       = existingUncertainMatch.Key,
                    IsActive = false,
                    Modified = DateTimeOffset.UtcNow
                };
                _context.Attach(uncertainMatchToDeactivate);
                var entry = _context.Entry(uncertainMatchToDeactivate);
                entry.Property(um => um.IsActive).IsModified = true;
                entry.Property(um => um.Modified).IsModified = true;
            }
        }

        void Merge(SlimAccommodationData existing)
        {
            ProcessDeactivation(accommodation.SupplierCode, DeactivationReasons.MatchingWithOther, accommodation);

            _accommodationChangeLogCollectorService.CollectMergeEvent(accommodationToUpdate.Id, AccommodationChangeEventSources.Updater,
                                                                      existing.HtId, matchedAccommodation.SupplierAccommodationCodes, existing.SupplierAccommodationCodes);

            foreach (var supplierCode in existing.SupplierAccommodationCodes)
            {
                accommodationToUpdate.SupplierAccommodationCodes.TryAdd(supplierCode.Key, supplierCode.Value);
            }

            AddOrUpdateHtAccommodationMappings(existing.HtId, matchedAccommodation.HtId);
            FindAndDeactivateUncertainMatch(matchedAccommodation.HtId, existing.HtId);
        }
    }