Skip to content

Commit

Permalink
refactor: generate signedUrl in toTaskDto
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jul 17, 2024
1 parent 470c066 commit 87ec7f9
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
magnito:
image: frourio/magnito:0.13.0
image: frourio/magnito:0.15.2
ports:
- 5050:5050
- 5051:5051
Expand Down
2 changes: 1 addition & 1 deletion server/api/private/tasks/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineController(() => ({
status: 200,
body: await taskQuery
.listByAuthorId(prismaClient, user.id, query?.limit)
.then((tasks) => tasks.map(toTaskDto)),
.then((tasks) => Promise.all(tasks.map(toTaskDto))),
}),
post: {
validators: { body: taskValidator.taskCreate },
Expand Down
2 changes: 1 addition & 1 deletion server/api/private/tasks/di/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default defineController({ listByAuthorId: taskQuery.listByAuthorId }, (d
status: 200,
body: await taskQuery.findManyWithDI
.inject(deps)(prismaClient, user.id)
.then((tasks) => tasks.map(toTaskDto)),
.then((tasks) => Promise.all(tasks.map(toTaskDto))),
}),
}));
3 changes: 2 additions & 1 deletion server/domain/task/model/taskEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type { TaskDto } from 'common/types/task';
import type { EntityId } from 'service/brandedId';
import type { S3PutParams } from 'service/s3Client';

export type TaskEntity = Omit<TaskDto, 'id' | 'author'> & {
export type TaskEntity = Omit<TaskDto, 'id' | 'image' | 'author'> & {
id: EntityId['task'];
imageKey: string | undefined;
author: Omit<TaskDto['author'], 'id'> & { id: EntityId['user'] };
};

Expand Down
13 changes: 4 additions & 9 deletions server/domain/task/model/taskMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@ import type { TaskUpdateDoneDto } from 'common/types/task';
import type { UserDto } from 'common/types/user';
import { labelValidator } from 'common/validators/task';
import { brandedId } from 'service/brandedId';
import { s3 } from 'service/s3Client';
import { ulid } from 'ulid';
import type { TaskCreateServerVal, TaskDeleteVal, TaskEntity, TaskSaveVal } from './taskEntity';

export const taskMethod = {
create: async (user: UserDto, val: TaskCreateServerVal): Promise<TaskSaveVal> => {
create: (user: UserDto, val: TaskCreateServerVal): TaskSaveVal => {
const task: TaskEntity = {
id: brandedId.task.entity.parse(ulid()),
done: false,
label: labelValidator.parse(val.label),
image: undefined,
imageKey: undefined,
createdTime: Date.now(),
author: { id: brandedId.user.entity.parse(user.id), signInName: user.signInName },
};

if (val.image === undefined) return { task };

const s3Key = `tasks/images/${ulid()}.${val.image.filename.split('.').at(-1)}`;
const url = await s3.getSignedUrl(s3Key);
const imageKey = `tasks/images/${ulid()}.${val.image.filename.split('.').at(-1)}`;

return {
task: { ...task, image: { s3Key, url } },
s3Params: { key: s3Key, data: val.image },
};
return { task: { ...task, imageKey }, s3Params: { key: imageKey, data: val.image } };
},
update: (user: UserDto, task: TaskEntity, dto: TaskUpdateDoneDto): TaskSaveVal => {
assert(user.id === String(task.author.id));
Expand Down
6 changes: 3 additions & 3 deletions server/domain/task/repository/taskCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export const taskCommand = {

await tx.task.upsert({
where: { id: val.task.id },
update: { label: val.task.label, done: val.task.done, imageKey: val.task.image?.s3Key },
update: { label: val.task.label, done: val.task.done, imageKey: val.task.imageKey },
create: {
id: val.task.id,
label: val.task.label,
done: val.task.done,
imageKey: val.task.image?.s3Key,
imageKey: val.task.imageKey,
createdAt: new Date(val.task.createdTime),
authorId: val.task.author.id,
},
Expand All @@ -25,6 +25,6 @@ export const taskCommand = {

await tx.task.delete({ where: { id: val.task.id } });

if (val.task.image !== undefined) await s3.delete(val.task.image.s3Key);
if (val.task.imageKey !== undefined) await s3.delete(val.task.imageKey);
},
};
10 changes: 3 additions & 7 deletions server/domain/task/repository/taskQuery.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import type { Prisma, Task, User } from '@prisma/client';
import type { DtoId, MaybeId } from 'common/types/brandedId';
import { brandedId } from 'service/brandedId';
import { s3 } from 'service/s3Client';
import { depend } from 'velona';
import type { TaskEntity } from '../model/taskEntity';

const toEntity = async (prismaTask: Task & { Author: User }): Promise<TaskEntity> => ({
const toEntity = (prismaTask: Task & { Author: User }): TaskEntity => ({
id: brandedId.task.entity.parse(prismaTask.id),
label: prismaTask.label,
done: prismaTask.done,
image:
prismaTask.imageKey === null
? undefined
: { url: await s3.getSignedUrl(prismaTask.imageKey), s3Key: prismaTask.imageKey },
imageKey: prismaTask.imageKey ?? undefined,
author: {
id: brandedId.user.entity.parse(prismaTask.authorId),
signInName: prismaTask.Author.signInName,
Expand All @@ -32,7 +28,7 @@ const listByAuthorId = async (
include: { Author: true },
});

return Promise.all(prismaTasks.map(toEntity));
return prismaTasks.map(toEntity);
};

export const taskQuery = {
Expand Down
6 changes: 5 additions & 1 deletion server/domain/task/service/toTaskDto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { TaskDto } from 'common/types/task';
import { brandedId } from 'service/brandedId';
import { s3 } from 'service/s3Client';
import type { TaskEntity } from '../model/taskEntity';

export const toTaskDto = (task: TaskEntity): TaskDto => ({
export const toTaskDto = async (task: TaskEntity): Promise<TaskDto> => ({
...task,
id: brandedId.task.dto.parse(task.id),
image: task.imageKey
? { s3Key: task.imageKey, url: await s3.getSignedUrl(task.imageKey) }
: undefined,
author: { ...task.author, id: brandedId.user.dto.parse(task.author.id) },
});
8 changes: 4 additions & 4 deletions server/domain/task/useCase/taskUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { toTaskDto } from '../service/toTaskDto';
export const taskUseCase = {
create: (user: UserDto, val: TaskCreateServerVal): Promise<TaskDto> =>
transaction('RepeatableRead', async (tx) => {
const created = await taskMethod.create(user, val);
const created = taskMethod.create(user, val);

await taskCommand.save(tx, created);

const dto = toTaskDto(created.task);
const dto = await toTaskDto(created.task);
taskEvent.created(user, dto);

return dto;
Expand All @@ -28,7 +28,7 @@ export const taskUseCase = {

await taskCommand.save(tx, updated);

const dto = toTaskDto(updated.task);
const dto = await toTaskDto(updated.task);
taskEvent.updated(user, dto);

return dto;
Expand All @@ -40,7 +40,7 @@ export const taskUseCase = {

await taskCommand.delete(tx, deleted);

const dto = toTaskDto(deleted.task);
const dto = await toTaskDto(deleted.task);
taskEvent.deleted(user, dto);

return dto;
Expand Down

0 comments on commit 87ec7f9

Please sign in to comment.