Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions apps/web/models/Widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,3 @@ export const WidgetSchema = new mongoose.Schema<WidgetInstance>({
shared: { type: Boolean, required: true, default: false },
settings: mongoose.Schema.Types.Mixed,
});

const WidgetModel =
mongoose.models.Widget || mongoose.model("Widget", WidgetSchema);

export default WidgetModel;
16 changes: 16 additions & 0 deletions packages/orm-models/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
env: {
node: true,
},
rules: {
"no-console": ["error", { allow: ["warn"] }],
},
ignorePatterns: ["dist/**/*.*"],
};
3 changes: 3 additions & 0 deletions packages/orm-models/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/

dist/
3 changes: 3 additions & 0 deletions packages/orm-models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@courselit/common-orm-models`

ORM models for CourseLit
1 change: 1 addition & 0 deletions packages/orm-models/additional.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "mongoose";
51 changes: 51 additions & 0 deletions packages/orm-models/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@courselit/orm-models",
"version": "0.0.1",
"description": "Common ORM models for CourseLit",
"author": "Team CourseLit <[email protected]>",
"homepage": "https://github.com/codelitdev/courselit#readme",
"private": true,
"license": "MIT",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/codelitdev/courselit.git"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1",
"clean": "rimraf dist/",
"prepublishOnly": "pnpm run build",
"build": "tsup",
"tsc:build": "tsc",
"dev": "tsup --watch",
"check-types": "tsc --noEmit"
},
"bugs": {
"url": "https://github.com/codelitdev/courselit/issues"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^8.46.0",
"@typescript-eslint/parser": "^8.46.0",
"tsconfig": "workspace:^",
"eslint": "^8.12.0",
"rimraf": "^4.1.1",
"tsup": "6.6.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@courselit/common-models": "workspace:^",
"@courselit/utils": "workspace:^",
"@courselit/email-editor": "workspace:^",
"@courselit/page-models": "workspace:^",
"mongoose": "^8.13.1"
}
}
34 changes: 34 additions & 0 deletions packages/orm-models/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export * from "./models/user";
export * from "./models/membership";
export * from "./models/media";
export * from "./models/sequence";
export * from "./models/user-segment";
export * from "./models/user-filter";
export * from "./models/course";
export * from "./models/rule";
export * from "./models/email";
export * from "./models/email-delivery";
export * from "./models/email-event";
export * from "./models/subscriber";
export * from "./models/domain";
export * from "./models/site-info";
export * from "./models/lesson";
export * from "./models/certificate";
export * from "./models/certificate-template";
export * from "./models/payment-plan";
export * from "./models/activity";
export * from "./models/lesson-evaluation";
export * from "./models/page";
export * from "./models/community";
export * from "./models/community-report";
export * from "./models/community-post-subscriber";
export * from "./models/community-comment";
export * from "./models/community-media";
export * from "./models/community-post";
export * from "./models/invoice";
export * from "./models/theme";
export * from "./models/ongoing-sequence";
export * from "./models/notification";
export * from "./models/download-link";
export * from "./models/apikey";
export * from "./models/user-theme";
31 changes: 31 additions & 0 deletions packages/orm-models/src/models/activity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mongoose from "mongoose";
import { ActivityType, Constants } from "@courselit/common-models";

export interface Activity {
domain: mongoose.Types.ObjectId;
userId: string;
type: ActivityType;
entityId?: string;
metadata?: Record<string, any>;
createdAt?: Date;
updatedAt?: Date;
}

export const ActivitySchema = new mongoose.Schema<Activity>(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
userId: { type: String, required: true },
type: {
type: String,
required: true,
enum: Object.values(Constants.ActivityType),
},
entityId: { type: String },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
},
{
timestamps: true,
},
);

ActivitySchema.index({ domain: 1, type: 1, createdAt: 1 });
29 changes: 29 additions & 0 deletions packages/orm-models/src/models/apikey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { generateUniqueId } from "@courselit/utils";
import mongoose from "mongoose";

export interface ApiKey {
domain: mongoose.Types.ObjectId;
keyId: string;
name: string;
key: string;
}

export const ApiKeySchema = new mongoose.Schema<ApiKey>(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
keyId: { type: String, required: true, default: generateUniqueId },
name: { type: String, required: true },
key: { type: String, required: true, default: generateUniqueId },
},
{
timestamps: true,
},
);

ApiKeySchema.index(
{
domain: 1,
name: 1,
},
{ unique: true },
);
43 changes: 43 additions & 0 deletions packages/orm-models/src/models/certificate-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Media } from "@courselit/common-models";
import { generateUniqueId } from "@courselit/utils";
import mongoose from "mongoose";
import { MediaSchema } from "./media";

export interface InternalCertificateTemplate {
domain: mongoose.Types.ObjectId;
templateId: string;
courseId: string;
title: string;
subtitle: string;
description: string;
signatureImage: Media;
signatureName: string;
signatureDesignation?: string;
logo?: Media;
createdAt: Date;
updatedAt: Date;
}

export const CertificateTemplateSchema =
new mongoose.Schema<InternalCertificateTemplate>(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
templateId: {
type: String,
required: true,
unique: true,
default: generateUniqueId,
},
courseId: { type: String, required: true },
title: { type: String, required: true },
subtitle: { type: String, required: true },
description: { type: String, required: true },
signatureImage: MediaSchema,
signatureName: { type: String, required: true },
signatureDesignation: { type: String },
logo: MediaSchema,
},
{
timestamps: true,
},
);
28 changes: 28 additions & 0 deletions packages/orm-models/src/models/certificate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { generateUniqueId } from "@courselit/utils";
import mongoose from "mongoose";

export interface InternalCertificate {
domain: mongoose.Types.ObjectId;
certificateId: string;
userId: string;
courseId: string;
createdAt: Date;
updatedAt: Date;
}

export const CertificateSchema = new mongoose.Schema<InternalCertificate>(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
certificateId: {
type: String,
required: true,
unique: true,
default: generateUniqueId,
},
userId: { type: String, required: true },
courseId: { type: String, required: true },
},
{
timestamps: true,
},
);
76 changes: 76 additions & 0 deletions packages/orm-models/src/models/community-comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { generateUniqueId } from "@courselit/utils";
import mongoose from "mongoose";
import { CommunityMediaSchema } from "./community-media";
import {
CommunityComment,
CommunityCommentReply,
} from "@courselit/common-models";

export interface InternalCommunityComment
extends Pick<
CommunityComment,
"communityId" | "postId" | "commentId" | "content" | "media"
> {
domain: mongoose.Types.ObjectId;
userId: string;
likes: string[];
replies: InternalReply[];
deleted: boolean;
}

export interface InternalReply
extends Omit<CommunityCommentReply, "likesCount" | "hasLiked"> {
userId: string;
likes: string[];
}

export const ReplySchema = new mongoose.Schema(
{
userId: { type: String, required: true },
content: { type: String, required: true },
media: [CommunityMediaSchema],
replyId: { type: String, required: true, default: generateUniqueId },
parentReplyId: { type: String, default: null },
likes: [String],
deleted: { type: Boolean, default: false },
},
{
timestamps: true,
},
);

export const CommunityCommentSchema =
new mongoose.Schema<InternalCommunityComment>(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
userId: { type: String, required: true },
communityId: { type: String, required: true },
postId: { type: String, required: true },
commentId: {
type: String,
required: true,
unique: true,
default: generateUniqueId,
},
content: { type: String, required: true },
media: [CommunityMediaSchema],
likes: [String],
replies: [ReplySchema],
deleted: { type: Boolean, required: true, default: false },
},
{
timestamps: true,
},
);

CommunityCommentSchema.statics.paginatedFind = async function (
filter,
options,
) {
const page = options.page || 1;
const limit = options.limit || 10;
const skip = (page - 1) * limit;

const docs = await this.find(filter).skip(skip).limit(limit).exec();
return docs;
};
10 changes: 10 additions & 0 deletions packages/orm-models/src/models/community-media.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import mongoose from "mongoose";
import { MediaSchema } from "./media";
import { CommunityMedia } from "@courselit/common-models";

export const CommunityMediaSchema = new mongoose.Schema<CommunityMedia>({
type: { type: String, required: true },
title: { type: String, required: true },
url: { type: String },
media: MediaSchema,
});
31 changes: 31 additions & 0 deletions packages/orm-models/src/models/community-post-subscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { generateUniqueId } from "@courselit/utils";
import mongoose from "mongoose";

export interface CommunityPostSubscriber {
domain: mongoose.Types.ObjectId;
subscriptionId: string;
postId: string;
userId: string;
subscription: boolean;
}

export const CommunityPostSubscriberSchema =
new mongoose.Schema<CommunityPostSubscriber>(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
subscriptionId: {
type: String,
required: true,
unique: true,
default: generateUniqueId,
},
postId: { type: String, required: true },
userId: { type: String, required: true },
subscription: { type: Boolean, default: true },
},
{
timestamps: true,
},
);

CommunityPostSubscriberSchema.index({ postId: 1, userId: 1 }, { unique: true });
Loading
Loading