use posts v2

This commit is contained in:
srgooglo 2022-10-13 21:56:29 +02:00
parent 75156dc201
commit 7e74e7c1cc
2 changed files with 25 additions and 9 deletions

View File

@ -54,14 +54,14 @@ export default class PostsController extends Controller {
"/post": { "/post": {
middlewares: ["withAuthentication"], middlewares: ["withAuthentication"],
fn: Schematized({ fn: Schematized({
required: ["message", "date"], required: ["timestamp"],
select: ["message", "additions", "type", "data", "date"], select: ["message", "attachments", "type", "data", "timestamp"],
}, async (req, res) => { }, async (req, res) => {
const post = await CreatePost({ const post = await CreatePost({
user_id: req.user.id, user_id: req.user.id,
message: req.selection.message, message: req.selection.message,
date: req.selection.date, timestamp: req.selection.timestamp,
additions: req.selection.additions, attachments: req.selection.attachments,
type: req.selection.type, type: req.selection.type,
data: req.selection.data, data: req.selection.data,
}) })

View File

@ -1,15 +1,31 @@
import { Post } from "../../../models" import { Post } from "../../../models"
import getPostData from "./getPostData" import getPostData from "./getPostData"
import momentTimezone from "moment-timezone"
export default async (payload) => { export default async (payload) => {
const { user_id, message, additions, type, data, date } = payload let { user_id, message, attachments, type, data, timestamp } = payload
// check if is a Array and have at least one element
const isAttachmentsValid = Array.isArray(attachments) && attachments.length > 0
if (!isAttachmentsValid && !message) {
throw new Error("Cannot create a post without message or attachments")
}
if (message) {
message = String(message).toString()
}
const current_timezone = momentTimezone.tz.guess()
const created_at = momentTimezone.tz(Date.now(), current_timezone).format()
const post = new Post({ const post = new Post({
user_id: typeof user_id === "object" ? user_id.toString() : user_id,
message: String(message).toString(),
additions: additions ?? [],
created_at: date,
type: type, type: type,
created_at: created_at,
user_id: typeof user_id === "object" ? user_id.toString() : user_id,
message: message,
attachments: attachments ?? [],
timestamp: timestamp,
data: data, data: data,
}) })