feat(server): user metadata (#9650)

* feat(server): user metadata

* add missing method to user mock

* update migration to include cascades

* update sql files

* test: fix e2e

* chore: clean up

---------

Co-authored-by: Daniel Dietzler <mail@ddietzler.dev>
This commit is contained in:
Jason Rasmussen
2024-05-22 08:13:36 -04:00
committed by GitHub
parent a4887bfa7e
commit 06ce8247cc
24 changed files with 267 additions and 126 deletions

View File

@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DummyValue, GenerateSql } from 'src/decorators';
import { AssetEntity } from 'src/entities/asset.entity';
import { UserMetadata, UserMetadataEntity, UserMetadataKey } from 'src/entities/user-metadata.entity';
import { UserEntity } from 'src/entities/user.entity';
import {
IUserRepository,
@@ -18,6 +19,7 @@ export class UserRepository implements IUserRepository {
constructor(
@InjectRepository(AssetEntity) private assetRepository: Repository<AssetEntity>,
@InjectRepository(UserEntity) private userRepository: Repository<UserEntity>,
@InjectRepository(UserMetadataEntity) private metadataRepository: Repository<UserMetadataEntity>,
) {}
async get(userId: string, options: UserFindOptions): Promise<UserEntity | null> {
@@ -25,6 +27,9 @@ export class UserRepository implements IUserRepository {
return this.userRepository.findOne({
where: { id: userId },
withDeleted: options.withDeleted,
relations: {
metadata: true,
},
});
}
@@ -69,6 +74,9 @@ export class UserRepository implements IUserRepository {
order: {
createdAt: 'DESC',
},
relations: {
metadata: true,
},
});
}
@@ -81,6 +89,13 @@ export class UserRepository implements IUserRepository {
return this.save({ ...user, id });
}
async upsertMetadata<T extends UserMetadataKey.PREFERENCES>(
id: string,
{ key, value }: { key: T; value: UserMetadata[T] },
) {
await this.metadataRepository.upsert({ userId: id, key, value }, { conflictPaths: { userId: true, key: true } });
}
async delete(user: UserEntity, hard?: boolean): Promise<UserEntity> {
return hard ? this.userRepository.remove(user) : this.userRepository.softRemove(user);
}
@@ -142,6 +157,12 @@ export class UserRepository implements IUserRepository {
private async save(user: Partial<UserEntity>) {
const { id } = await this.userRepository.save(user);
return this.userRepository.findOneOrFail({ where: { id }, withDeleted: true });
return this.userRepository.findOneOrFail({
where: { id },
withDeleted: true,
relations: {
metadata: true,
},
});
}
}