diff --git a/functions/common/middlewares.js b/functions/common/middlewares.js index 0033c54..4a747d5 100644 --- a/functions/common/middlewares.js +++ b/functions/common/middlewares.js @@ -20,7 +20,9 @@ const getUser = async (event) => { }; const getUserV2 = async (event) => { - const token = event["multiValueHeaders"]["Authorization"]; + const token = + event["multiValueHeaders"]["Authorization"] || + event["multiValueHeaders"]["authorization"]; if (!token) throw new Error("No JWT"); diff --git a/functions/create-post-v2.js b/functions/create-post-v2.js index 5100a44..9caae50 100644 --- a/functions/create-post-v2.js +++ b/functions/create-post-v2.js @@ -3,7 +3,7 @@ const { generateSlug, headers } = require("./common/helpers"); const { getUserV2 } = require("./common/middlewares"); const connectToDb = require("../db"); const User = require("../server/api/user/model"); -const Model = require("../server/api/post/model"); +const PostModel = require("../server/api/post/model"); exports.handler = async (event) => { try { @@ -35,7 +35,7 @@ exports.handler = async (event) => { }; }); - const result = await Model.create(posts); + const result = await PostModel.create(posts); await User.findOneAndUpdate( { _id }, diff --git a/functions/get-posts.js b/functions/get-posts.js index 5932650..b1191f5 100644 --- a/functions/get-posts.js +++ b/functions/get-posts.js @@ -1,10 +1,14 @@ -const { connectToDb } = require("./common/db"); +const connectToDb = require("../db"); const { headers } = require("./common/helpers"); +const PostModel = require("../server/api/post/model"); +const { getUserV2 } = require("./common/middlewares"); exports.handler = async (event) => { try { - const db = await connectToDb(); - const collection = db.collection("posts"); + await connectToDb(); + + const user = await getUserV2(event); + const { _id } = user; const { search, @@ -18,6 +22,7 @@ exports.handler = async (event) => { collectionId, deleted: false, visible: true, + userId: _id, }; const sort = { @@ -32,45 +37,43 @@ exports.handler = async (event) => { if (search) { const regex = new RegExp(search, "gi"); - aggregation["title"] = { - $regex: regex, - }; - aggregation["content"] = { - $regex: regex, - }; + aggregation["$or"] = [ + { title: { $regex: regex } }, + { content: { $regex: regex } }, + ]; } - const posts = await collection - .aggregate([ - { $match: aggregation }, - { $sort: sort }, - { - $skip: (Number(page) - 1) * Number(limit), - }, - { $limit: Number(limit) }, - { - $project: { - _id: 1, - tags: 1, - type: 1, - title: 1, - content: 1, - slug: 1, - solution: 1, - createdAt: 1, - }, - }, - ]) - .toArray(); + const posts = await PostModel.aggregate([ + { $match: aggregation }, + { $sort: sort }, + { + $skip: (Number(page) - 1) * Number(limit), + }, + { $limit: Number(limit) }, + // { + // $project: { + // _id: 1, + // tags: 1, + // type: 1, + // status: 1, + // title: 1, + // content: 1, + // slug: 1, + // createdAt: 1, + // collectionId: 1 + // }, + // }, + ]); - const count = await collection.find(aggregation).count(); + const count = await PostModel.find(aggregation).count(); return { statusCode: 200, - body: JSON.stringify({ posts, meta: { count } }, undefined, 2), + body: JSON.stringify({ posts, meta: { count } }), headers, }; } catch (err) { - return { statusCode: 500, body: err.toString() }; + console.log(err); + return { statusCode: 200, body: err.toString(), headers }; } };