mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-07-23 04:26:57 +00:00
Zone collection + creation
Zone create UI flow
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
"use server";
|
||||
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
import { auth } from "@/auth";
|
||||
import { zoneSchema } from "@/schemas";
|
||||
import { collections, dbConnect } from "@/server/mongodb";
|
||||
import { errorLog } from "@/utils/logger";
|
||||
|
||||
import { ZoneModel } from "./models/zoneModel";
|
||||
import { action } from "./safeAction";
|
||||
|
||||
export const createZone = action(zoneSchema, async (input) => {
|
||||
try {
|
||||
await dbConnect();
|
||||
|
||||
const user = await auth();
|
||||
if (!user?.user) {
|
||||
throw new Error("You must be logged in to create a zone");
|
||||
}
|
||||
|
||||
const zoneData: ZoneModel = {
|
||||
...input,
|
||||
userId: new ObjectId(user.user!.id!),
|
||||
};
|
||||
|
||||
const result = await collections.zones!.insertOne(zoneData);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
errorLog(error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
import { zu } from "@infra-blocks/zod-utils";
|
||||
import { ObjectId } from "mongodb";
|
||||
import { z } from "zod";
|
||||
|
||||
const featureCollection = zu.geojson.featureCollection();
|
||||
|
||||
export type ZoneModel = {
|
||||
userId: ObjectId;
|
||||
name: string;
|
||||
classicNotifications: boolean;
|
||||
rapidNotifications: boolean;
|
||||
blitzNotifications: boolean;
|
||||
features: z.infer<typeof featureCollection>;
|
||||
};
|
||||
@@ -10,6 +10,7 @@ export const collections: {
|
||||
tournaments?: mongoDB.Collection<TournamentModel>;
|
||||
clubs?: mongoDB.Collection<ClubModel>;
|
||||
users?: mongoDB.Collection<UserModel>;
|
||||
zones?: mongoDB.Collection<ZoneModel>;
|
||||
} = {};
|
||||
|
||||
if (!process.env.MONGODB_URI) {
|
||||
@@ -34,6 +35,7 @@ export async function dbConnect() {
|
||||
|
||||
const userData: mongoDB.Db = client!.db("userData");
|
||||
collections.users = userData.collection("userData");
|
||||
collections.zones = userData.collection("zones");
|
||||
|
||||
const tournamentData: mongoDB.Db = client!.db("tournamentsFranceDB");
|
||||
collections.tournaments = tournamentData.collection("tournaments");
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
"use server";
|
||||
|
||||
import { omit } from "lodash";
|
||||
import { ObjectId } from "mongodb";
|
||||
import { z } from "zod";
|
||||
|
||||
import { auth } from "@/auth";
|
||||
import { collections, dbConnect } from "@/server/mongodb";
|
||||
|
||||
import { action } from "./safeAction";
|
||||
|
||||
export const myZones = action(z.void(), async () => {
|
||||
await dbConnect();
|
||||
|
||||
const user = await auth();
|
||||
if (!user?.user) {
|
||||
throw new Error("You must be logged in to fetch your zones");
|
||||
}
|
||||
|
||||
const zones = await collections
|
||||
.zones!.find({ userId: new ObjectId(user.user!.id!) })
|
||||
.toArray();
|
||||
|
||||
return zones.map((zone) => ({
|
||||
...omit(zone, ["_id"]),
|
||||
id: zone._id.toString(),
|
||||
userId: zone.userId.toString(),
|
||||
}));
|
||||
});
|
||||
Reference in New Issue
Block a user