refactor: migrate memory to kysely (#15314)

This commit is contained in:
Jason Rasmussen
2025-01-15 11:34:11 -05:00
committed by GitHub
parent 43b3181f45
commit 93e2545275
5 changed files with 177 additions and 89 deletions

View File

@@ -69,7 +69,17 @@ describe(MemoryService.name, () => {
memoryAt: new Date(2024),
}),
).resolves.toMatchObject({ assets: [] });
expect(memoryMock.create).toHaveBeenCalledWith(expect.objectContaining({ assets: [] }));
expect(memoryMock.create).toHaveBeenCalledWith(
{
ownerId: 'admin_id',
memoryAt: expect.any(Date),
type: MemoryType.ON_THIS_DAY,
isSaved: undefined,
sendAt: undefined,
data: { year: 2024 },
},
new Set(),
);
});
it('should create a memory', async () => {
@@ -80,14 +90,14 @@ describe(MemoryService.name, () => {
type: MemoryType.ON_THIS_DAY,
data: { year: 2024 },
assetIds: ['asset1'],
memoryAt: new Date(2024),
memoryAt: new Date(2024, 0, 1),
}),
).resolves.toBeDefined();
expect(memoryMock.create).toHaveBeenCalledWith(
expect.objectContaining({
ownerId: userStub.admin.id,
assets: [{ id: 'asset1' }],
}),
new Set(['asset1']),
);
});
@@ -115,12 +125,7 @@ describe(MemoryService.name, () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
memoryMock.update.mockResolvedValue(memoryStub.memory1);
await expect(sut.update(authStub.admin, 'memory1', { isSaved: true })).resolves.toBeDefined();
expect(memoryMock.update).toHaveBeenCalledWith(
expect.objectContaining({
id: 'memory1',
isSaved: true,
}),
);
expect(memoryMock.update).toHaveBeenCalledWith('memory1', expect.objectContaining({ isSaved: true }));
});
});

View File

@@ -2,7 +2,6 @@ import { BadRequestException, Injectable } from '@nestjs/common';
import { BulkIdResponseDto, BulkIdsDto } from 'src/dtos/asset-ids.response.dto';
import { AuthDto } from 'src/dtos/auth.dto';
import { MemoryCreateDto, MemoryResponseDto, MemoryUpdateDto, mapMemory } from 'src/dtos/memory.dto';
import { AssetEntity } from 'src/entities/asset.entity';
import { Permission } from 'src/enum';
import { BaseService } from 'src/services/base.service';
import { addAssets, removeAssets } from 'src/utils/asset.util';
@@ -29,15 +28,17 @@ export class MemoryService extends BaseService {
permission: Permission.ASSET_SHARE,
ids: assetIds,
});
const memory = await this.memoryRepository.create({
ownerId: auth.user.id,
type: dto.type,
data: dto.data,
isSaved: dto.isSaved,
memoryAt: dto.memoryAt,
seenAt: dto.seenAt,
assets: [...allowedAssetIds].map((id) => ({ id }) as AssetEntity),
});
const memory = await this.memoryRepository.create(
{
ownerId: auth.user.id,
type: dto.type,
data: dto.data,
isSaved: dto.isSaved,
memoryAt: dto.memoryAt,
seenAt: dto.seenAt,
},
allowedAssetIds,
);
return mapMemory(memory);
}
@@ -45,8 +46,7 @@ export class MemoryService extends BaseService {
async update(auth: AuthDto, id: string, dto: MemoryUpdateDto): Promise<MemoryResponseDto> {
await this.requireAccess({ auth, permission: Permission.MEMORY_UPDATE, ids: [id] });
const memory = await this.memoryRepository.update({
id,
const memory = await this.memoryRepository.update(id, {
isSaved: dto.isSaved,
memoryAt: dto.memoryAt,
seenAt: dto.seenAt,
@@ -68,7 +68,7 @@ export class MemoryService extends BaseService {
const hasSuccess = results.find(({ success }) => success);
if (hasSuccess) {
await this.memoryRepository.update({ id, updatedAt: new Date() });
await this.memoryRepository.update(id, { updatedAt: new Date() });
}
return results;
@@ -86,7 +86,7 @@ export class MemoryService extends BaseService {
const hasSuccess = results.find(({ success }) => success);
if (hasSuccess) {
await this.memoryRepository.update({ id, updatedAt: new Date() });
await this.memoryRepository.update(id, { id, updatedAt: new Date() });
}
return results;