Skip to content

Commit

Permalink
refactor(content): validate parent_id without checking another field
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafatcb committed Jun 22, 2024
1 parent f26a478 commit 11e7d84
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions models/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async function create(postedContent, options = {}) {
transaction: options.transaction,
});

throwIfParentIsNotInPath(newContent);
throwIfSpecifiedParentDoesNotExist(postedContent, newContent);

await creditOrDebitTabCoins(null, newContent, {
eventId: options.eventId,
Expand All @@ -306,6 +306,7 @@ async function create(postedContent, options = {}) {
WITH
parent AS (
SELECT
id,
owner_id,
CASE
WHEN id IS NULL THEN ARRAY[]::uuid[]
Expand All @@ -325,7 +326,7 @@ async function create(postedContent, options = {}) {
SELECT
inserted_content.id,
inserted_content.owner_id,
inserted_content.parent_id,
parent.id as parent_id,
inserted_content.slug,
inserted_content.title,
inserted_content.body,
Expand Down Expand Up @@ -401,8 +402,8 @@ function populateStatus(postedContent) {
postedContent.status = postedContent.status || 'draft';
}

function throwIfParentIsNotInPath(newContent) {
if (newContent.parent_id && newContent.path.at(-1) !== newContent.parent_id) {
function throwIfSpecifiedParentDoesNotExist(postedContent, newContent) {
if (postedContent.parent_id && postedContent.parent_id !== newContent.parent_id) {
throw new ValidationError({
message: `Você está tentando criar um comentário em um conteúdo que não existe.`,
action: `Utilize um "parent_id" que aponte para um conteúdo existente.`,
Expand Down Expand Up @@ -545,7 +546,6 @@ async function creditOrDebitTabCoins(oldContent, newContent, options = {}) {
}

// We should not credit if the content has little or no value.
// Expected 5 or more words with 5 or more characters.
if (newContent.body.split(/[a-z]{5,}/i, 6).length < 6) return;

if (newContent.parent_id) {
Expand Down

0 comments on commit 11e7d84

Please sign in to comment.