diff --git a/src/app/lib/actions/entityManagement/postActions.ts b/src/app/lib/actions/entityManagement/postActions.ts index 1314175..03c1f16 100644 --- a/src/app/lib/actions/entityManagement/postActions.ts +++ b/src/app/lib/actions/entityManagement/postActions.ts @@ -3,15 +3,10 @@ import { revalidatePath, revalidateTag } from 'next/cache' import { Bucket, Post, PostBucket, User, dbSync } from "@/model/Models"; import { Attributes, where } from "@sequelize/core"; -import { cookies } from 'next/headers'; import { getCookieAuth, userIsAdmin } from '../actions'; import { ActionResult } from '../ActionResult'; import { PostAttributesWithBuckets } from '@/model/Post'; - - - - export async function deletePost(postID: number): Promise> { await dbSync; // revalidatePath("/admin/man-post","page") @@ -20,16 +15,14 @@ export async function deletePost(postID: number): Promise> return {result: true}; } - -export async function getPostsWithBuckets(): Promise> { +export async function getPostsWithBucketsAndProject(): Promise> { await dbSync; if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."} - const posts = await Post.findAll({include: {association: Post.associations.buckets, include: Bucket.associations.attachments}},); + const posts = await Post.findAll({include: [{association: Post.associations.buckets, include: Bucket.associations.attachments}, {association: Post.associations.project}], nest: false}); + console.dir(JSON.parse(JSON.stringify(posts)),{depth: 10, colors: true}) return {result:JSON.parse(JSON.stringify(posts))}; } - - export async function getPosts(): Promise[]>> { await dbSync; if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."} @@ -37,7 +30,6 @@ export async function getPosts(): Promise[]>> { return {result:JSON.parse(JSON.stringify(posts))}; } - export async function updatePost(postAttributes: Partial>): Promise[]>> { await dbSync; if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."} diff --git a/src/components/client/EntityManagementTable.tsx b/src/components/client/EntityManagementTable.tsx index 9e08590..8034501 100644 --- a/src/components/client/EntityManagementTable.tsx +++ b/src/components/client/EntityManagementTable.tsx @@ -9,6 +9,7 @@ type TableGenOptions = { type Props = { headings:Array; children:ReactNode; + entityName:string; options?:TableGenOptions } @@ -16,6 +17,7 @@ export default function EntityManagementTable(props:Props){ const options = props.options ? props.options : {editButton: true, deleteButton: true} console.log(props.headings) return <> +

{props.entityName} Management

diff --git a/src/components/client/admin/PostTable.tsx b/src/components/client/admin/PostTable.tsx index 5f42be0..44b5230 100644 --- a/src/components/client/admin/PostTable.tsx +++ b/src/components/client/admin/PostTable.tsx @@ -20,7 +20,7 @@ import { useState } from "react"; import { Project, Post, Bucket } from "@/model/Models"; import { ActionResult } from "@/app/lib/actions/ActionResult"; import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; -import { getPostsWithBuckets } from "@/app/lib/actions/entityManagement/postActions"; +import { getPostsWithBucketsAndProject } from "@/app/lib/actions/entityManagement/postActions"; import { PostAttributesWithBuckets } from "@/model/Post"; type Actions = { @@ -100,7 +100,7 @@ export default function PostTable(props: Props) { project_id: e.project_id }) .then(res => handleActionResult(res)) - .then(getPostsWithBuckets) + .then(getPostsWithBucketsAndProject) .then(res => { if (!res.error && res.result) setPosts(res.result); }) @@ -169,7 +169,7 @@ export default function PostTable(props: Props) { } return <> - + {posts.map((postData: Partial) => )} diff --git a/src/components/server/admin/views/PostView.tsx b/src/components/server/admin/views/PostView.tsx index 5c1d10e..9dbfef6 100644 --- a/src/components/server/admin/views/PostView.tsx +++ b/src/components/server/admin/views/PostView.tsx @@ -5,7 +5,7 @@ import { constructAPIUrl } from "@/util/Utils"; import { ReactNode, useEffect } from "react"; import EntityManagementTable from "../../../client/EntityManagementTable"; import PostTable from "@/components/client/admin/PostTable"; -import { deletePost, getPostsWithBuckets, updatePost } from "@/app/lib/actions/entityManagement/postActions"; +import { deletePost, getPostsWithBucketsAndProject, updatePost } from "@/app/lib/actions/entityManagement/postActions"; import { getProjects } from "@/app/lib/actions/entityManagement/projectActions"; import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models"; import { tryCreateAttachment } from "@/app/api/attachment/route"; @@ -13,22 +13,20 @@ import { tryCreateAttachment } from "@/app/api/attachment/route"; type Props = { children?:ReactNode } - +async function getHeadings(){ + let headings:string[] = ['#', 'Title', 'Content', 'Project', 'CreatedAt', 'UpdatedAt'] + + return headings +} export default async function PostView(props:Props){ - await dbSync; + const sync = await dbSync; + + const headings = await getHeadings(); - const headings = [ - '#', - 'Title', - 'Content', - 'Project', - 'Date Created', - 'Date Modified', - ] const actions = { deletePost: deletePost, - getPosts:getPostsWithBuckets, + getPosts:getPostsWithBucketsAndProject, getProjects:getProjects, savePost:updatePost, // uploadAttachment:tryCreateAttachment @@ -39,7 +37,6 @@ export default async function PostView(props:Props){ return (
-

Post Management

posts.map((e)=>JSON.parse(JSON.stringify(e)))); const projects = await Project.findAll().then(projects=>projects.map((e)=>JSON.parse(JSON.stringify(e)))); - + const headings = await getHeadings(); return <>
- {(()=>{ - let a:string[] = [] - for (const key in headings) - { - a.push(key) - } - // return a.map((e)=>
{e}
); - return <> - })()} + <> {/* await sequelize.sync())().then(()=>{ addUserScopes(); addUserPermsScopes(); + addPostScopes(); }); diff --git a/src/model/Post.ts b/src/model/Post.ts index c42f595..dc147a6 100644 --- a/src/model/Post.ts +++ b/src/model/Post.ts @@ -95,8 +95,8 @@ export class PostBucket extends Model, InferCreation declare post?:NonAttribute declare bucket?:NonAttribute +} - - - - } +export function addPostScopes(){ + Post.addScope('withProject', {include: [{association: Post.associations.project}]}) +}