Skip to content

Commit

Permalink
fix: #445
Browse files Browse the repository at this point in the history
  • Loading branch information
meetqy committed Oct 10, 2023
1 parent 3e6bed0 commit 0d3885e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from "zod";
import { DEFAULT_THEME } from "@rao-pics/constant";
import { prisma } from "@rao-pics/db";

import { folderCore } from "./folder";
import { t } from "./utils";

export const configInput = {
Expand All @@ -28,7 +29,11 @@ export const configCore = {
// 2.1 如果修改的是父级 folder, 则需要同步修改子级 folder
// 3. 查询 Folder 时,只需要返回 folder.show = true 的 folder
upsert: async (input: z.infer<typeof configInput.upsert>) => {
// const { pwdFolder } = input;
const { pwdFolder } = input;

if (pwdFolder != undefined) {
await folderCore.setPwdFolderShow(pwdFolder);
}

return await prisma.config.upsert({
where: { name: "config" },
Expand Down
25 changes: 23 additions & 2 deletions packages/api/src/folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,34 @@ export const folderCore = {
});
},

/**
* 修改所有有密码的文件夹的 show
* 子级的 show 保持和父级一致
* 父级有密码,子项没有,子项也会设置为 父级 相同的 show
*/
setPwdFolderShow: async (
input: z.infer<typeof folderInput.setPwdFolderShow>,
) => {
// 所有有密码的文件夹
const pFolders = await prisma.folder.findMany({
where: { password: { not: "" } },
});

// 父级文件有密码、子级没有密码的文件夹 id
const pids = pFolders.filter((f) => !f.pid).map((f) => f.id);

const childFolders = await prisma.folder.findMany({
where: {
pid: {
in: pids,
},
},
});

return await prisma.folder.updateMany({
where: {
password: {
not: "",
id: {
in: pFolders.concat(childFolders).map((item) => item.id),
},
},
data: {
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { z } from "zod";
import type { Pending } from "@rao-pics/db";

import { router } from "../..";
import { configCore } from "../config";
import { folderCore } from "../folder";
import { t } from "../utils";
import { handleFolder } from "./folder";
import { deleteImage, upsertImage } from "./image";
Expand Down Expand Up @@ -53,6 +55,8 @@ export const sync = t.router({
// 同步文件夹
const folders = handleFolder(join(input.libraryPath, "metadata.json"));
await syncFolder(folders, caller);
const config = await configCore.findUnique();
await folderCore.setPwdFolderShow(config?.pwdFolder ?? false);

Check warning on line 59 in packages/api/src/sync/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/api/src/sync/index.ts#L58-L59

Added lines #L58 - L59 were not covered by tests
} catch (e) {
// 每次同步首先同步文件夹,如果文件夹同步失败,直接返回
return false;
Expand Down

0 comments on commit 0d3885e

Please sign in to comment.