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": {
middlewares: ["withAuthentication"],
fn: Schematized({
required: ["message", "date"],
select: ["message", "additions", "type", "data", "date"],
required: ["timestamp"],
select: ["message", "attachments", "type", "data", "timestamp"],
}, async (req, res) => {
const post = await CreatePost({
user_id: req.user.id,
message: req.selection.message,
date: req.selection.date,
additions: req.selection.additions,
timestamp: req.selection.timestamp,
attachments: req.selection.attachments,
type: req.selection.type,
data: req.selection.data,
})

View File

@ -1,15 +1,31 @@
import { Post } from "../../../models"
import getPostData from "./getPostData"
import momentTimezone from "moment-timezone"
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({
user_id: typeof user_id === "object" ? user_id.toString() : user_id,
message: String(message).toString(),
additions: additions ?? [],
created_at: date,
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,
})