improve share logic
This commit is contained in:
@ -29,6 +29,13 @@
|
||||
"when": 1714541233589,
|
||||
"tag": "0003_pale_overlord",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 4,
|
||||
"version": "5",
|
||||
"when": 1714565073180,
|
||||
"tag": "0004_cuddly_wolf_cub",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
@ -110,7 +110,7 @@ export default {
|
||||
},
|
||||
});
|
||||
if (!sb) return;
|
||||
return { id: sb.id, name: sb.name, type: sb.type, author: sb.author.name, sharedOn: Date.now() };
|
||||
return { id: sb.id, name: sb.name, type: sb.type, author: sb.author.name, sharedOn: r.sharedOn };
|
||||
})
|
||||
);
|
||||
|
||||
@ -128,6 +128,7 @@ export default {
|
||||
const user = await db.query.user.findFirst({
|
||||
where: (user, { eq }) => eq(user.email, email),
|
||||
with: {
|
||||
sandbox: true,
|
||||
usersToSandboxes: true,
|
||||
},
|
||||
});
|
||||
@ -136,11 +137,15 @@ export default {
|
||||
return new Response("No user associated with email.", { status: 400 });
|
||||
}
|
||||
|
||||
if (user.sandbox.find((sb) => sb.id === sandboxId)) {
|
||||
return new Response("Cannot share with yourself!", { status: 400 });
|
||||
}
|
||||
|
||||
if (user.usersToSandboxes.find((uts) => uts.sandboxId === sandboxId)) {
|
||||
return new Response("User already has access.", { status: 400 });
|
||||
}
|
||||
|
||||
await db.insert(usersToSandboxes).values({ userId: user.id, sandboxId }).get();
|
||||
await db.insert(usersToSandboxes).values({ userId: user.id, sandboxId, sharedOn: new Date() }).get();
|
||||
|
||||
return success;
|
||||
} else if (method === "DELETE") {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
import { createId } from "@paralleldrive/cuid2";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { relations, sql } from "drizzle-orm";
|
||||
|
||||
export const user = sqliteTable("user", {
|
||||
id: text("id")
|
||||
@ -49,6 +49,7 @@ export const usersToSandboxes = sqliteTable("users_to_sandboxes", {
|
||||
sandboxId: text("sandboxId")
|
||||
.notNull()
|
||||
.references(() => sandbox.id),
|
||||
sharedOn: integer("sharedOn", { mode: "timestamp_ms" }),
|
||||
});
|
||||
|
||||
export const usersToSandboxesRelations = relations(usersToSandboxes, ({ one }) => ({
|
||||
|
Reference in New Issue
Block a user