2025-01-14 03:24:24 +01:00

202 lines
5.3 KiB
TypeScript

"use server";
import { cookies } from "next/headers";
import { APIError } from "@/util/api/error";
import {
Attachment,
Auth,
Bucket,
DBState,
Post,
PostTag,
Project,
Tag,
User,
UserPerms,
dbSync,
sequelize,
} from "@/models";
import Sequelize, { CreationAttributes, DataTypes } from "@sequelize/core";
import {
SqliteColumnsDescription,
SqliteDialect,
SqliteQueryInterface,
} from "@sequelize/sqlite3";
import { hashpassword } from "@/util/auth";
import { copyFileSync, mkdirSync, readFileSync } from "fs";
import path from "path";
import { UUID } from "crypto";
import { runApiAction } from "@/util/api";
async function seedUsers(qif: SqliteQueryInterface<SqliteDialect>) {
const fp = path.resolve("./db/seed/users.json");
const json: { users: CreationAttributes<User>[] } = JSON.parse(
Buffer.from(readFileSync(fp).valueOf()).toString()
);
const users = json.users.map(async (user) => {
user.password = await hashpassword(user.password);
return user;
});
const dbUsers = await User.bulkCreate(await Promise.all(users), {
include: User.associations.perms,
});
}
async function seedPosts(qif: SqliteQueryInterface<SqliteDialect>) {
const fp = path.resolve("./db/seed/posts.json");
const json: { users: CreationAttributes<User>[] } = JSON.parse(
Buffer.from(readFileSync(fp).valueOf()).toString()
);
const projects = [
{
name: "Blog",
readableIdentifier: "blog",
posts: [
{
title: "Test Post",
content:
"# Hello <ExampleComponent />\nthis is some **test** markdown and we make some edits\n![](/attachment/788dfc19-55ba-482c-8124-277702296dfb/FB_IMG_1716665756868.jpg)",
description: "A new post to test the blog system",
project_id: 1,
user_id: 1,
buckets: [
{
id: "788dfc19-55ba-482c-8124-277702296dfb",
attachments: [
{
filename: "FB_IMG_1716665756868.jpg",
},
],
},
],
},
],
},
];
projects.map(async (project) => {
await Project.create({
name: project.name,
readableIdentifier: project.readableIdentifier,
});
project.posts.map(async (post) => {
const pst = await Post.create({
title: post.title,
content: post.content,
description: post.description,
project_id: post.project_id,
user_id: post.user_id,
});
post.buckets.map(async (bucket) => {
await pst.createBucket({ id: bucket.id as UUID });
bucket.attachments.map(async (attachment) => {
await Attachment.create({
bucket_id: bucket.id as UUID,
filename: attachment.filename,
}).then(async (a) => {
console.log(bucket.id);
mkdirSync(`./bucket/${bucket.id}/`, { recursive: true });
copyFileSync(
path.resolve(".", "db", "seed", "posts", a.filename),
path.resolve(".", "bucket", bucket.id, a.filename)
);
return;
});
});
});
});
});
const project = await Project.findOne({
where: {
readableIdentifier: "blog",
},
}).then(
async (project) =>
project ||
(await Project.create({
name: "General Blog",
readableIdentifier: "blog",
}))
);
}
async function seedDatabase(qif: SqliteQueryInterface<SqliteDialect>) {
await seedUsers(qif);
await seedPosts(qif);
return;
// await Post.create({
// title: 'Test Post',
// content: `
// # Hello <ExampleComponent />
// this is some **test** markdown
// `,
// project_id: project.id,
// user_id: user.id
// })
// await Post.create({
// title: 'Test Post 2',
// content: `
// # Hello <ExampleComponent />
// this is amother post with some **test** markdown
// `,
// project_id: project.id,
// user_id: user.id
// })
}
export async function tryGET(request: Request) {
return await runApiAction(async (request) => {
await dbSync;
const queryInterface = sequelize.queryInterface;
const version = (await DBState.findAll())
.sort((a, b) => (a.version > b.version ? 1 : -1))
.map((a) => a.version)[0];
switch (version) {
case 1:
break;
default:
await seedDatabase(queryInterface);
const postsRows: SqliteColumnsDescription = await queryInterface
.describeTable(Post.table.tableName)
.then((t) => t);
if (!postsRows["project_id"])
await queryInterface.addColumn("Posts", "project_id", {
type: DataTypes.INTEGER,
acceptsNull: () => false,
defaultValue: 1,
});
break;
}
return new Response(
JSON.stringify({
test: await queryInterface.describeTable("Posts").then((t) => t),
}),
{
status: 200,
headers: {
"Content-Type": "text/JSON",
},
}
);
}, request);
}
export async function GET(request: Request) {
try {
return await tryGET(request);
} catch (e) {
if (e instanceof APIError) {
return new Response(e.info.responseText, { status: e.info.status });
} else {
return new Response(JSON.stringify(e));
throw e;
}
}
}