Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion backend/src/core/api/draft/draft.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DraftService } from 'core/modules/draft/services';
import { CreateDraftDto } from 'core/modules/draft/dto';
import { CreateDraftDto, UpdateDraftDto } from 'core/modules/draft/dto';
import { ValidHttpResponse } from 'packages/handler/response/validHttp.response';

class Controller {
Expand All @@ -13,6 +13,15 @@ class Controller {
const data = await this.service.createDraft(userId, dto);
return ValidHttpResponse.toOkResponse(data);
};

updateDraft = async req => {
const userId = req.user.payload.id;
const draftId = req.params.id;
const dto = UpdateDraftDto(req.body);
const data = await this.service.updateDraft(userId, draftId, dto);
return ValidHttpResponse.toOkResponse(data);
};
}

export const DraftController = new Controller();

15 changes: 14 additions & 1 deletion backend/src/core/api/draft/draft.resolver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Module } from 'packages/handler/Module';
import { CreateDraftInterceptor } from 'core/modules/draft/interceptor';
import { CreateDraftInterceptor, UpdateDraftInterceptor } from 'core/modules/draft/interceptor';
import { RecordIdInterceptor } from 'core/modules/interceptor/recordId/record-id.interceptor';
import { RecordId } from 'core/common/swagger';
import { DraftController } from './draft.controller';

export const DraftResolver = Module.builder()
Expand All @@ -16,5 +18,16 @@ export const DraftResolver = Module.builder()
body: 'CreateDraftDto',
controller: DraftController.createDraft,
preAuthorization: true
},
{
route: '/:id',
method: 'put',
params: [RecordId],
interceptors: [RecordIdInterceptor, UpdateDraftInterceptor],
body: 'UpdateDraftDto',
controller: DraftController.updateDraft,
preAuthorization: true
}
]);


1 change: 1 addition & 0 deletions backend/src/core/modules/draft/dto/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './create-draft.dto';
export * from './update-draft.dto';
10 changes: 10 additions & 0 deletions backend/src/core/modules/draft/dto/update-draft.dto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiDocument } from 'core/config/swagger.config';
import { SwaggerDocument } from 'packages/swagger';

ApiDocument.addModel('UpdateDraftDto', {
content: SwaggerDocument.ApiProperty({ type: 'object', required: false }),
});

export const UpdateDraftDto = body => ({
content: body.content || {},
});
1 change: 1 addition & 0 deletions backend/src/core/modules/draft/interceptor/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './create-draft.interceptor';
export * from './update-draft.interceptor';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Joi from 'joi';
import { DefaultValidatorInterceptor } from 'core/infrastructure/interceptor';

export const UpdateDraftInterceptor = new DefaultValidatorInterceptor(
Joi.object({
content: Joi.object().optional().allow(null),
})
);
31 changes: 31 additions & 0 deletions backend/src/core/modules/draft/services/draft.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@ class Service {
throw new InternalServerException(error.message);
}
}

async updateDraft(userId, draftId, payload) {
try {
const { content } = payload;

const existing = await prisma.documents.findFirst({
where: {
id: draftId,
user_id: userId,
is_draft: true,
deleted_at: null
}
});

if (!existing) {
throw new NotFoundException(`Draft with ID "${draftId}" not found`);
}

return await prisma.documents.update({
where: { id: draftId },
data: {
content: content || {},
updated_at: new Date()
}
});
} catch (error) {
if (error instanceof NotFoundException) throw error;
throw new InternalServerException(error.message);
}
}
}


export const DraftService = new Service();
Loading