This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refactor' into develop
- Loading branch information
Showing
11 changed files
with
158 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
services/utils/contentParser.js → services/contentParser/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// ------------------------------------- // | ||
// Number Validation // | ||
// ------------------------------------- // | ||
function textIsNumber(value) { | ||
if (typeof value === "string" && value.trim() !== "") { | ||
return !isNaN(value); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function textToNumber(value) { | ||
return parseFloat(value); | ||
} | ||
|
||
// -------------------------------------- // | ||
// Boolean Validation // | ||
// -------------------------------------- // | ||
const booleanStringPossibleValues = { | ||
trueValues: ["true", "t"], | ||
falseValues: ["false", "f"], | ||
}; | ||
|
||
function textIsBoolean(value) { | ||
return ( | ||
booleanStringPossibleValues.trueValues.includes(value.toLowerCase()) || | ||
booleanStringPossibleValues.falseValues.includes(value.toLowerCase()) | ||
); | ||
} | ||
|
||
function textToBoolean(value) { | ||
return booleanStringPossibleValues.trueValues.includes(value); | ||
} | ||
|
||
// ------------------------------------- // | ||
// Object Validation // | ||
// ------------------------------------- // | ||
function textIsObject(value) { | ||
try { | ||
JSON.parse(value); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
function textToObject(value) { | ||
return JSON.parse(value); | ||
} | ||
|
||
// --------------------------- // | ||
// Exports // | ||
// --------------------------- // | ||
module.exports = { | ||
textIsNumber, | ||
textToNumber, | ||
textIsBoolean, | ||
textToBoolean, | ||
textIsObject, | ||
textToObject, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,56 @@ | ||
const { isNumber, isString, urlIsMedia } = require("./formatsValidator"); | ||
const { MANY_RELATIONS } = require("../../constants/relations"); | ||
const { importMediaFromUrl } = require("./importMediaFiles"); | ||
const { urlIsMedia } = require("./formatsValidator"); | ||
const { importMediaFromUrl } = require("../importer/importMediaFiles"); | ||
|
||
function getId(value) { | ||
if (isNumber(value)) return parseInt(value); | ||
return value?.id || null; | ||
if (typeof value === "number") return value; | ||
if (typeof value === "object" && value.id) return value.id; | ||
return null; | ||
} | ||
|
||
async function getValidContent({ value, attribute, user }) { | ||
const { type } = attribute; | ||
|
||
if (type === "media") { | ||
const { multiple } = attribute; | ||
if (multiple) { | ||
if (!Array.isArray(value)) return []; | ||
const urls = value.filter((v) => isString(v) && urlIsMedia(v)); | ||
const uploadedFiles = await Promise.all( | ||
urls.map((url) => importMediaFromUrl(url, user)) | ||
); | ||
|
||
const ids = value.map(getId).filter((v) => v !== null); | ||
const entities = await strapi | ||
.query("file", "upload") | ||
.find({ id_in: ids }); | ||
|
||
return [...uploadedFiles, ...entities.map(({ id }) => id)]; | ||
} else { | ||
if (Array.isArray(value)) return null; | ||
|
||
// Upload url to plugin upload | ||
if (isString(value) && urlIsMedia(value)) { | ||
return importMediaFromUrl(value, user); | ||
} | ||
|
||
const id = getId(value); | ||
const entity = await strapi.query("file", "upload").findOne({ id }); | ||
return entity?.id || null; | ||
} | ||
async function getValidRelations(value, attribute) { | ||
const { relationType, targetModel } = attribute; | ||
if (MANY_RELATIONS.includes(relationType)) { | ||
const relations = Array.isArray(value) ? value : [value]; | ||
const ids = relations.map(getId); | ||
const entities = await strapi.query(targetModel).find({ id_in: ids }); | ||
return entities.map(({ id }) => id); | ||
} else { | ||
const relation = Array.isArray(value) ? value[0] : value; | ||
const id = getId(relation); | ||
const entity = await strapi.query(targetModel).findOne({ id }); | ||
return entity ? entity.id : null; | ||
} | ||
} | ||
|
||
if (type === "relation") { | ||
const { relationType, targetModel } = attribute; | ||
|
||
if (MANY_RELATIONS.includes(relationType)) { | ||
if (!Array.isArray(value)) return []; | ||
const ids = value.map(getId); | ||
|
||
const entities = await strapi.query(targetModel).find({ id_in: ids }); | ||
return entities.map(({ id }) => id); | ||
} else { | ||
if (Array.isArray(value)) return null; | ||
|
||
const id = getId(value); | ||
const entity = await strapi.query(targetModel).findOne({ id }); | ||
return entity?.id || null; | ||
async function getValidMedia(value, attribute, user) { | ||
const { multiple } = attribute; | ||
if (multiple) { | ||
const medias = Array.isArray(value) ? value : [value]; | ||
const urls = medias.filter((v) => urlIsMedia(v)); | ||
const uploadedFiles = await Promise.all( | ||
urls.map((url) => importMediaFromUrl(url, user)) | ||
); | ||
|
||
const ids = medias.map(getId).filter((v) => v !== null); | ||
const entities = await strapi.query("file", "upload").find({ id_in: ids }); | ||
|
||
return [...uploadedFiles, ...entities.map(({ id }) => id)]; | ||
} else { | ||
const media = Array.isArray(value) ? value[0] : value; | ||
|
||
// Upload url to plugin upload | ||
if (urlIsMedia(media)) { | ||
return importMediaFromUrl(media, user); | ||
} | ||
} | ||
|
||
// Other types are not validated | ||
return value; | ||
const id = getId(media); | ||
const entity = await strapi.query("file", "upload").findOne({ id }); | ||
return entity ? entity.id : null; | ||
} | ||
} | ||
|
||
module.exports = { | ||
getValidContent, | ||
getValidRelations, | ||
getValidMedia, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.