Files
butcem/src/models/household-member.ts
T
2026-11-05 02:14:10 +03:00

27 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { model, models, Schema, type InferSchemaType, type Model } from "mongoose";
import { HOUSEHOLD_ROLES } from "@/features/households/types";
/** Kullanıcının bir hanedeki üyeliği ve hane içi rolü (owner | member | viewer). */
const householdMemberSchema = new Schema(
{
householdId: { type: Schema.Types.ObjectId, ref: "Household", required: true },
/** Better Auth `user` koleksiyonundaki _id. */
userId: { type: Schema.Types.ObjectId, required: true },
role: { type: String, enum: HOUSEHOLD_ROLES, required: true },
createdBy: { type: Schema.Types.ObjectId, required: true },
updatedBy: { type: Schema.Types.ObjectId, required: true },
},
{ timestamps: true, collection: "householdMembers" },
);
// Bir kullanıcı bir hanede yalnızca bir kez üye olabilir.
householdMemberSchema.index({ householdId: 1, userId: 1 }, { unique: true });
// Kullanıcının hanelerini hızlı listelemek için.
householdMemberSchema.index({ userId: 1, householdId: 1 });
export type HouseholdMember = InferSchemaType<typeof householdMemberSchema>;
export const HouseholdMemberModel: Model<HouseholdMember> =
(models.HouseholdMember as Model<HouseholdMember> | undefined) ??
model<HouseholdMember>("HouseholdMember", householdMemberSchema);