initial commit
This commit is contained in:
1 parent
7cb985116e
commit
75acf7b38f
955 files changed
+37333
-87054
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export interface IInvite extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
orgRoleId?: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
teamRoleId: Types.ObjectId;
|
||||
email: string;
|
||||
tokenHash: string;
|
||||
createdBy: Types.ObjectId;
|
||||
updatedBy: Types.ObjectId;
|
||||
expiry: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const InviteSchema = new Schema<IInvite>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
orgRoleId: { type: Schema.Types.ObjectId, ref: "Role" },
|
||||
teamRoleId: { type: Schema.Types.ObjectId, ref: "Role", required: true },
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true,
|
||||
lowercase: true,
|
||||
},
|
||||
|
||||
tokenHash: { type: String, required: true, unique: true },
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: () => new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
expires: 0,
|
||||
},
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
updatedBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Invite = mongoose.model<IInvite>("Invite", InviteSchema);
|
||||
@@ -0,0 +1,18 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
export interface IOrg extends Document {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
ownerId: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const orgSchema = new Schema<IOrg>(
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
ownerId: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Org = mongoose.model<IOrg>("Org", orgSchema);
|
||||
@@ -0,0 +1,51 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { invalidateCachesForUser } from "@/middleware/AddUserContext.js";
|
||||
|
||||
export interface IOrgMembership extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
userId: Types.ObjectId;
|
||||
roleId?: Types.ObjectId;
|
||||
role: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const orgMembershipSchema = new Schema<IOrgMembership>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
userId: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
roleId: { type: Schema.Types.ObjectId, ref: "Role" },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
orgMembershipSchema.post<IOrgMembership>("save", async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
orgMembershipSchema.post<IOrgMembership>(
|
||||
"deleteOne",
|
||||
{ document: true, query: false },
|
||||
async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
orgMembershipSchema.post("findOneAndDelete", function (doc: IOrgMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
orgMembershipSchema.post(
|
||||
"findOneAndUpdate",
|
||||
async function (doc: IOrgMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
export const OrgMembership = mongoose.model<IOrgMembership>(
|
||||
"OrgMembership",
|
||||
orgMembershipSchema
|
||||
);
|
||||
@@ -0,0 +1,26 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export const ScopeTypes = ["organization", "team"] as const;
|
||||
export type ScopeType = (typeof ScopeTypes)[number];
|
||||
|
||||
export interface IRole extends Document {
|
||||
_id: Types.ObjectId;
|
||||
organizationId: Types.ObjectId;
|
||||
name: string;
|
||||
scope: ScopeType;
|
||||
permissions: string[];
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const roleSchema = new Schema<IRole>(
|
||||
{
|
||||
organizationId: { type: Schema.Types.ObjectId, ref: "Org" },
|
||||
name: { type: String, required: true },
|
||||
scope: { type: String, enum: ScopeTypes, required: true },
|
||||
permissions: { type: [String], default: [] },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Role = mongoose.model<IRole>("Role", roleSchema);
|
||||
@@ -0,0 +1,22 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
export interface ITeam extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
name: string;
|
||||
description?: string;
|
||||
isSystem?: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const teamSchema = new Schema<ITeam>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
name: { type: String, required: true },
|
||||
description: { type: String, required: false },
|
||||
isSystem: { type: Boolean, default: false },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Team = mongoose.model<ITeam>("Team", teamSchema);
|
||||
@@ -0,0 +1,51 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { invalidateCachesForUser } from "@/middleware/AddUserContext.js";
|
||||
export interface ITeamMembership extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
userId: Types.ObjectId;
|
||||
roleId: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const teamMembershipSchema = new Schema<ITeamMembership>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
userId: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
roleId: { type: Schema.Types.ObjectId, ref: "Role" },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
teamMembershipSchema.post<ITeamMembership>("save", async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
teamMembershipSchema.post<ITeamMembership>(
|
||||
"deleteOne",
|
||||
{ document: true, query: false },
|
||||
async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
teamMembershipSchema.post("findOneAndDelete", function (doc: ITeamMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
teamMembershipSchema.post(
|
||||
"findOneAndUpdate",
|
||||
async function (doc: ITeamMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
export const TeamMembership = mongoose.model<ITeamMembership>(
|
||||
"TeamMembership",
|
||||
teamMembershipSchema
|
||||
);
|
||||
@@ -0,0 +1,67 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { IRole, ITeam } from "@/db/models/index.js";
|
||||
|
||||
export interface ITokenizedUser {
|
||||
sub: string;
|
||||
email: string;
|
||||
orgId: string;
|
||||
}
|
||||
export interface IUserContext {
|
||||
sub: string;
|
||||
email: string;
|
||||
orgId: string;
|
||||
teamIds?: string[];
|
||||
teams?: ITeam[];
|
||||
currentTeamId?: string;
|
||||
roles?: {
|
||||
orgRole?: IRole;
|
||||
teamRole: IRole;
|
||||
};
|
||||
}
|
||||
|
||||
export interface IUserReturnable {
|
||||
id: string;
|
||||
email: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
org: {
|
||||
name: string;
|
||||
permissions: string[];
|
||||
};
|
||||
teams: {
|
||||
id: string;
|
||||
name: string;
|
||||
permissions: string[];
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface IUser extends Document {
|
||||
_id: Types.ObjectId;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
passwordHash: string;
|
||||
lastLoginAt?: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface IUserProfile {
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
password?: string;
|
||||
confirmPassword?: string;
|
||||
}
|
||||
|
||||
const userSchema = new Schema<IUser>(
|
||||
{
|
||||
firstName: { type: String, required: true },
|
||||
lastName: { type: String, required: true },
|
||||
email: { type: String, required: true, unique: true },
|
||||
passwordHash: { type: String, required: true },
|
||||
lastLoginAt: { type: Date },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const User = mongoose.model<IUser>("User", userSchema);
|
||||
Reference in new issue
Block a user