improved typesafety

This commit is contained in:
Daniel
2023-05-27 07:41:29 +03:30
parent 7f3d93517d
commit f26ffa7323
12 changed files with 321 additions and 535 deletions
@@ -4,10 +4,13 @@
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
import { prisma } from "@/lib/api/db";
import { NewCollection } from "@/types/global";
import { CollectionIncludingMembers } from "@/types/global";
import { existsSync, mkdirSync } from "fs";
export default async function (collection: NewCollection, userId: number) {
export default async function (
collection: CollectionIncludingMembers,
userId: number
) {
if (!collection || collection.name.trim() === "")
return {
response: "Please enter a valid collection.",
@@ -43,7 +46,7 @@ export default async function (collection: NewCollection, userId: number) {
description: collection.description,
members: {
create: collection.members.map((e) => ({
user: { connect: { email: e.email } },
user: { connect: { email: e.user.email } },
canCreate: e.canCreate,
canUpdate: e.canUpdate,
canDelete: e.canDelete,
@@ -4,11 +4,14 @@
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
import { prisma } from "@/lib/api/db";
import { ExtendedCollection } from "@/types/global";
import { CollectionIncludingMembers } from "@/types/global";
import getPermission from "@/lib/api/getPermission";
export default async function (collection: ExtendedCollection, userId: number) {
if (!collection)
export default async function (
collection: CollectionIncludingMembers,
userId: number
) {
if (!collection.id)
return { response: "Please choose a valid collection.", status: 401 };
const collectionIsAccessible = await getPermission(userId, collection.id);
+19 -29
View File
@@ -1,15 +1,15 @@
import { ExtendedCollection, NewCollection } from "@/types/global";
import { CollectionIncludingMembers, Member } from "@/types/global";
import getPublicUserDataByEmail from "./getPublicUserDataByEmail";
const addMemberToCollection = async (
ownerEmail: string,
memberEmail: string,
collection: ExtendedCollection,
setMemberState: Function,
collectionMethod: "ADD" | "UPDATE"
collection: CollectionIncludingMembers,
setMember: (newMember: Member) => null | undefined
) => {
const checkIfMemberAlreadyExists = collection.members.find((e: any) => {
const email = collectionMethod === "ADD" ? e.email : e.user.email;
console.log(collection.members);
const checkIfMemberAlreadyExists = collection.members.find((e) => {
const email = e.user.email;
return email === memberEmail;
});
@@ -24,30 +24,20 @@ const addMemberToCollection = async (
// Lookup, get data/err, list ...
const user = await getPublicUserDataByEmail(memberEmail.trim());
if (user.email) {
const newMember =
collectionMethod === "ADD"
? {
id: user.id,
name: user.name,
email: user.email,
canCreate: false,
canUpdate: false,
canDelete: false,
}
: {
collectionId: collection.id,
userId: user.id,
canCreate: false,
canUpdate: false,
canDelete: false,
user: {
name: user.name,
email: user.email,
},
};
console.log(collection);
setMemberState(newMember);
if (user.email) {
setMember({
collectionId: collection.id,
userId: user.id,
canCreate: false,
canUpdate: false,
canDelete: false,
user: {
name: user.name,
email: user.email,
},
});
}
}
};