Skip to content

Commit

Permalink
refactor: access repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 committed Jan 21, 2025
1 parent 887267b commit fb8010a
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 152 deletions.
53 changes: 0 additions & 53 deletions server/src/interfaces/access.interface.ts

This file was deleted.

129 changes: 57 additions & 72 deletions server/src/repositories/access.repository.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
import { Injectable } from '@nestjs/common';
import { Kysely, sql } from 'kysely';
import { InjectKysely } from 'nestjs-kysely';
import { DB } from 'src/db';
import { ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';

import { AlbumUserRole } from 'src/enum';
import { IAccessRepository } from 'src/interfaces/access.interface';
import { asUuid } from 'src/utils/database';

type IActivityAccess = IAccessRepository['activity'];
type IAlbumAccess = IAccessRepository['album'];
type IAssetAccess = IAccessRepository['asset'];
type IAuthDeviceAccess = IAccessRepository['authDevice'];
type IMemoryAccess = IAccessRepository['memory'];
type IPersonAccess = IAccessRepository['person'];
type IPartnerAccess = IAccessRepository['partner'];
type IStackAccess = IAccessRepository['stack'];
type ITagAccess = IAccessRepository['tag'];
type ITimelineAccess = IAccessRepository['timeline'];

@Injectable()
class ActivityAccess implements IActivityAccess {
class ActivityAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, activityIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, activityIds: Set<string>) {
if (activityIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -44,9 +29,9 @@ class ActivityAccess implements IActivityAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkAlbumOwnerAccess(userId: string, activityIds: Set<string>): Promise<Set<string>> {
async checkAlbumOwnerAccess(userId: string, activityIds: Set<string>) {
if (activityIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -61,9 +46,9 @@ class ActivityAccess implements IActivityAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkCreateAccess(userId: string, albumIds: Set<string>): Promise<Set<string>> {
async checkCreateAccess(userId: string, albumIds: Set<string>) {
if (albumIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -80,14 +65,14 @@ class ActivityAccess implements IActivityAccess {
}
}

class AlbumAccess implements IAlbumAccess {
class AlbumAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, albumIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, albumIds: Set<string>) {
if (albumIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -102,9 +87,9 @@ class AlbumAccess implements IAlbumAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkSharedAlbumAccess(userId: string, albumIds: Set<string>, access: AlbumUserRole): Promise<Set<string>> {
async checkSharedAlbumAccess(userId: string, albumIds: Set<string>, access: AlbumUserRole) {
if (albumIds.size === 0) {
return new Set();
return new Set<string>();
}

const accessRole =
Expand All @@ -125,9 +110,9 @@ class AlbumAccess implements IAlbumAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkSharedLinkAccess(sharedLinkId: string, albumIds: Set<string>): Promise<Set<string>> {
async checkSharedLinkAccess(sharedLinkId: string, albumIds: Set<string>) {
if (albumIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -142,14 +127,14 @@ class AlbumAccess implements IAlbumAccess {
}
}

class AssetAccess implements IAssetAccess {
class AssetAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkAlbumAccess(userId: string, assetIds: Set<string>): Promise<Set<string>> {
async checkAlbumAccess(userId: string, assetIds: Set<string>) {
if (assetIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand Down Expand Up @@ -185,9 +170,9 @@ class AssetAccess implements IAssetAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, assetIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, assetIds: Set<string>) {
if (assetIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -201,9 +186,9 @@ class AssetAccess implements IAssetAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkPartnerAccess(userId: string, assetIds: Set<string>): Promise<Set<string>> {
async checkPartnerAccess(userId: string, assetIds: Set<string>) {
if (assetIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -224,9 +209,9 @@ class AssetAccess implements IAssetAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkSharedLinkAccess(sharedLinkId: string, assetIds: Set<string>): Promise<Set<string>> {
async checkSharedLinkAccess(sharedLinkId: string, assetIds: Set<string>) {
if (assetIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand Down Expand Up @@ -276,14 +261,14 @@ class AssetAccess implements IAssetAccess {
}
}

class AuthDeviceAccess implements IAuthDeviceAccess {
class AuthDeviceAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, deviceIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, deviceIds: Set<string>) {
if (deviceIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -296,14 +281,14 @@ class AuthDeviceAccess implements IAuthDeviceAccess {
}
}

class StackAccess implements IStackAccess {
class StackAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, stackIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, stackIds: Set<string>) {
if (stackIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -316,14 +301,14 @@ class StackAccess implements IStackAccess {
}
}

class TimelineAccess implements ITimelineAccess {
class TimelineAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkPartnerAccess(userId: string, partnerIds: Set<string>): Promise<Set<string>> {
async checkPartnerAccess(userId: string, partnerIds: Set<string>) {
if (partnerIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -336,14 +321,14 @@ class TimelineAccess implements ITimelineAccess {
}
}

class MemoryAccess implements IMemoryAccess {
class MemoryAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, memoryIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, memoryIds: Set<string>) {
if (memoryIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -357,14 +342,14 @@ class MemoryAccess implements IMemoryAccess {
}
}

class PersonAccess implements IPersonAccess {
class PersonAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, personIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, personIds: Set<string>) {
if (personIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -378,9 +363,9 @@ class PersonAccess implements IPersonAccess {

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkFaceOwnerAccess(userId: string, assetFaceIds: Set<string>): Promise<Set<string>> {
async checkFaceOwnerAccess(userId: string, assetFaceIds: Set<string>) {
if (assetFaceIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -396,14 +381,14 @@ class PersonAccess implements IPersonAccess {
}
}

class PartnerAccess implements IPartnerAccess {
class PartnerAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkUpdateAccess(userId: string, partnerIds: Set<string>): Promise<Set<string>> {
async checkUpdateAccess(userId: string, partnerIds: Set<string>) {
if (partnerIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -416,14 +401,14 @@ class PartnerAccess implements IPartnerAccess {
}
}

class TagAccess implements ITagAccess {
class TagAccess {
constructor(private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID_SET] })
@ChunkedSet({ paramIndex: 1 })
async checkOwnerAccess(userId: string, tagIds: Set<string>): Promise<Set<string>> {
async checkOwnerAccess(userId: string, tagIds: Set<string>) {
if (tagIds.size === 0) {
return new Set();
return new Set<string>();
}

return this.db
Expand All @@ -436,17 +421,17 @@ class TagAccess implements ITagAccess {
}
}

export class AccessRepository implements IAccessRepository {
activity: IActivityAccess;
album: IAlbumAccess;
asset: IAssetAccess;
authDevice: IAuthDeviceAccess;
memory: IMemoryAccess;
person: IPersonAccess;
partner: IPartnerAccess;
stack: IStackAccess;
tag: ITagAccess;
timeline: ITimelineAccess;
export class AccessRepository {
activity: ActivityAccess;
album: AlbumAccess;
asset: AssetAccess;
authDevice: AuthDeviceAccess;
memory: MemoryAccess;
person: PersonAccess;
partner: PartnerAccess;
stack: StackAccess;
tag: TagAccess;
timeline: TimelineAccess;

constructor(@InjectKysely() db: Kysely<DB>) {
this.activity = new ActivityAccess(db);
Expand Down
3 changes: 1 addition & 2 deletions server/src/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IAccessRepository } from 'src/interfaces/access.interface';
import { IAlbumUserRepository } from 'src/interfaces/album-user.interface';
import { IAlbumRepository } from 'src/interfaces/album.interface';
import { IKeyRepository } from 'src/interfaces/api-key.interface';
Expand Down Expand Up @@ -78,11 +77,11 @@ import { ViewRepository } from 'src/repositories/view-repository';

export const repositories = [
//
AccessRepository,
ActivityRepository,
];

export const providers = [
{ provide: IAccessRepository, useClass: AccessRepository },
{ provide: IAlbumRepository, useClass: AlbumRepository },
{ provide: IAlbumUserRepository, useClass: AlbumUserRepository },
{ provide: IAssetRepository, useClass: AssetRepository },
Expand Down
Loading

0 comments on commit fb8010a

Please sign in to comment.