Skip to content

Commit

Permalink
Fix: Get post lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
318097 committed Mar 11, 2021
1 parent 828b44a commit d4dcfd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
4 changes: 3 additions & 1 deletion functions/common/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions functions/create-post-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -35,7 +35,7 @@ exports.handler = async (event) => {
};
});

const result = await Model.create(posts);
const result = await PostModel.create(posts);

await User.findOneAndUpdate(
{ _id },
Expand Down
71 changes: 37 additions & 34 deletions functions/get-posts.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,6 +22,7 @@ exports.handler = async (event) => {
collectionId,
deleted: false,
visible: true,
userId: _id,
};

const sort = {
Expand All @@ -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 };
}
};

0 comments on commit d4dcfd7

Please sign in to comment.