<commit_before>import { Injectable, NgZone } from '@angular/core';
import { NgSerializerService } from '@kaiu/ng-serializer';
import { PendingChangesService } from './pending-changes/pending-changes.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { FirestoreRelationalStorage } from './storage/firestore/firestore-relational-storage';
import { UserInventory } from '../../model/user/inventory/user-inventory';
import { AuthFacade } from '../../+state/auth.facade';
import { Observable } from 'rxjs';
import { TeamcraftUser } from '../../model/user/teamcraft-user';
import { debounceTime, map, shareReplay, switchMap, tap } from 'rxjs/operators';
import { ContainerType } from '../../model/user/inventory/container-type';
import { diff } from 'deep-diff';

@Injectable({
  providedIn: 'root'
})
export class UserInventoryService extends FirestoreRelationalStorage<UserInventory> {

  private userInventory$ = this.authFacade.user$.pipe(
    switchMap(user => {
      return this.getByForeignKey(TeamcraftUser, user.$key).pipe(
        map((inventories: UserInventory[]) => {
          if (user.defaultLodestoneId) {
            return inventories.find(inventory => inventory.characterId === user.defaultLodestoneId);
          }
          return inventories[0];
        }),
        map(inventory => {
          if (inventory === undefined) {
            const newInventory = new UserInventory();
            newInventory.authorId = user.$key;
            newInventory.characterId = user.defaultLodestoneId;
            return newInventory;
          }
          return inventory;
        })
      );
    }),
    debounceTime(200),
    shareReplay(1)
  );

  constructor(protected firestore: AngularFirestore, protected serializer: NgSerializerService, protected zone: NgZone,
              protected pendingChangesService: PendingChangesService, private authFacade: AuthFacade) {
    super(firestore, serializer, zone, pendingChangesService);
  }

  public getContainerName(containerId: number): string {
    switch (containerId) {
      case ContainerType.Bag0:
      case ContainerType.Bag1:
      case ContainerType.Bag2:
      case ContainerType.Bag3:
        return 'Bag';
      case ContainerType.RetainerBag0:
      case ContainerType.RetainerBag1:
      case ContainerType.RetainerBag2:
      case ContainerType.RetainerBag3:
      case ContainerType.RetainerBag4:
      case ContainerType.RetainerBag5:
      case ContainerType.RetainerBag6:
        return 'RetainerBag';
      case ContainerType.SaddleBag0:
      case ContainerType.SaddleBag1:
        return 'SaddleBag';
    }
    return 'Other';
  }

  update(uid: string, data: Partial<UserInventory>, uriParams?: any): Observable<void> {
    const changes = diff(this.syncCache[uid], data);
    if (changes.some(entry => entry.kind === 'D' || entry.kind === 'A')) {
      return super.update(uid, data, uriParams);
    }
    const patch = changes.reduce((res, change) => {
      res[change.path.join('.')] = change.rhs;
      return res;
    }, {});
    return super.update(uid, patch, uriParams);
  }

  public getUserInventory(): Observable<UserInventory> {
    return this.userInventory$;
  }

  protected getBaseUri(): string {
    return '/user-inventories';<commit_msg>chore: fix for inventory update when no changes happened<commit_after>import { Injectable, NgZone } from '@angular/core';
import { NgSerializerService } from '@kaiu/ng-serializer';
import { PendingChangesService } from './pending-changes/pending-changes.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { FirestoreRelationalStorage } from './storage/firestore/firestore-relational-storage';
import { UserInventory } from '../../model/user/inventory/user-inventory';
import { AuthFacade } from '../../+state/auth.facade';
import { Observable } from 'rxjs';
import { TeamcraftUser } from '../../model/user/teamcraft-user';
import { debounceTime, map, shareReplay, switchMap, tap } from 'rxjs/operators';
import { ContainerType } from '../../model/user/inventory/container-type';
import { diff } from 'deep-diff';

@Injectable({
  providedIn: 'root'
})
export class UserInventoryService extends FirestoreRelationalStorage<UserInventory> {

  private userInventory$ = this.authFacade.user$.pipe(
    switchMap(user => {
      return this.getByForeignKey(TeamcraftUser, user.$key).pipe(
        map((inventories: UserInventory[]) => {
          if (user.defaultLodestoneId) {
            return inventories.find(inventory => inventory.characterId === user.defaultLodestoneId);
          }
          return inventories[0];
        }),
        map(inventory => {
          if (inventory === undefined) {
            const newInventory = new UserInventory();
            newInventory.authorId = user.$key;
            newInventory.characterId = user.defaultLodestoneId;
            return newInventory;
          }
          return inventory;
        })
      );
    }),
    debounceTime(200),
    shareReplay(1)
  );

  constructor(protected firestore: AngularFirestore, protected serializer: NgSerializerService, protected zone: NgZone,
              protected pendingChangesService: PendingChangesService, private authFacade: AuthFacade) {
    super(firestore, serializer, zone, pendingChangesService);
  }

  public getContainerName(containerId: number): string {
    switch (containerId) {
      case ContainerType.Bag0:
      case ContainerType.Bag1:
      case ContainerType.Bag2:
      case ContainerType.Bag3:
        return 'Bag';
      case ContainerType.RetainerBag0:
      case ContainerType.RetainerBag1:
      case ContainerType.RetainerBag2:
      case ContainerType.RetainerBag3:
      case ContainerType.RetainerBag4:
      case ContainerType.RetainerBag5:
      case ContainerType.RetainerBag6:
        return 'RetainerBag';
      case ContainerType.SaddleBag0:
      case ContainerType.SaddleBag1:
        return 'SaddleBag';
    }
    return 'Other';
  }

  update(uid: string, data: Partial<UserInventory>, uriParams?: any): Observable<void> {
    const changes = (diff(this.syncCache[uid], data) || []);
    if (changes.some(entry => entry.kind === 'D' || entry.kind === 'A')) {
      return super.update(uid, data, uriParams);
    }
    const patch = changes.reduce((res, change) => {
      res[change.path.join('.')] = change.rhs;
      return res;
    }, {});
    return super.update(uid, patch, uriParams);
  }

  public getUserInventory(): Observable<UserInventory> {
    return this.userInventory$;
  }

  protected getBaseUri(): string {
    return '/user-inventories';
{'fraction_non_alphanumeric': 0.12281767117048732, 'fraction_numerical': 0.005465310459996963, 'mean_word_length': 3.3086984957488554, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494061', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1866, 'n_words': 403}