Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import UserModel from "@models/User";
import CommunityPostSubscriberModel from "@models/CommunityPostSubscriber";
import constants from "@/config/constants";
import { Constants, TextEditorContent } from "@courselit/common-models";
import {
CommunityPostRepository,
UserRepository,
MembershipRepository,
PaymentPlanRepository,
PageRepository,
CommunityRepository,
DomainRepository,
} from "@courselit/orm-models";

const communityPostRepo = new CommunityPostRepository(CommunityPostModel);
const communityRepo = new CommunityRepository(CommunityModel);
const domainRepo = new DomainRepository(DomainModel);
const membershipRepo = new MembershipRepository(MembershipModel);
const pageRepo = new PageRepository(PageModel);
const paymentPlanRepo = new PaymentPlanRepository(PaymentPlanModel);
const userRepo = new UserRepository(UserModel);

jest.mock("@/services/medialit");
jest.mock("@/services/queue");
Expand Down Expand Up @@ -47,12 +64,12 @@ describe("createCommunityPost", () => {
let commentOnlyCtx: any;

beforeAll(async () => {
testDomain = await DomainModel.create({
testDomain = await domainRepo.create({
name: id("domain"),
email: email("domain"),
});

adminUser = await UserModel.create({
adminUser = await userRepo.create({
domain: testDomain._id,
userId: id("admin"),
email: email("admin"),
Expand All @@ -62,7 +79,7 @@ describe("createCommunityPost", () => {
unsubscribeToken: id("unsub-admin"),
});

regularUser = await UserModel.create({
regularUser = await userRepo.create({
domain: testDomain._id,
userId: id("regular"),
email: email("regular"),
Expand All @@ -72,7 +89,7 @@ describe("createCommunityPost", () => {
unsubscribeToken: id("unsub-regular"),
});

commentOnlyUser = await UserModel.create({
commentOnlyUser = await userRepo.create({
domain: testDomain._id,
userId: id("comment-only"),
email: email("comment-only"),
Expand All @@ -83,7 +100,7 @@ describe("createCommunityPost", () => {
});

// Internal payment plan (required by the system)
await PaymentPlanModel.create({
await paymentPlanRepo.create({
domain: testDomain._id,
planId: id("internal-plan"),
userId: adminUser.userId,
Expand All @@ -97,7 +114,7 @@ describe("createCommunityPost", () => {
currencyISOCode: "USD",
});

community = await CommunityModel.create({
community = await communityRepo.create({
domain: testDomain._id,
communityId: id("community"),
name: "Test Community",
Expand All @@ -108,7 +125,7 @@ describe("createCommunityPost", () => {
categories: ["General", "Announcements"],
});

await PageModel.create({
await pageRepo.create({
domain: testDomain._id,
pageId: community.pageId,
type: constants.communityPage,
Expand All @@ -118,7 +135,7 @@ describe("createCommunityPost", () => {
});

// Free payment plan for the community
await PaymentPlanModel.create({
await paymentPlanRepo.create({
domain: testDomain._id,
planId: id("free-plan"),
userId: adminUser.userId,
Expand All @@ -132,7 +149,7 @@ describe("createCommunityPost", () => {
});

// Admin membership (MODERATE role)
await MembershipModel.create({
await membershipRepo.create({
domain: testDomain._id,
membershipId: id("admin-membership"),
userId: adminUser.userId,
Expand All @@ -145,7 +162,7 @@ describe("createCommunityPost", () => {
});

// Regular user membership (POST role)
await MembershipModel.create({
await membershipRepo.create({
domain: testDomain._id,
membershipId: id("regular-membership"),
userId: regularUser.userId,
Expand All @@ -158,7 +175,7 @@ describe("createCommunityPost", () => {
});

// Comment-only user membership (COMMENT role — cannot post)
await MembershipModel.create({
await membershipRepo.create({
domain: testDomain._id,
membershipId: id("comment-membership"),
userId: commentOnlyUser.userId,
Expand Down Expand Up @@ -229,7 +246,7 @@ describe("createCommunityPost", () => {
});

it("should throw action_not_allowed for a user with no membership", async () => {
const noMemberUser = await UserModel.create({
const noMemberUser = await userRepo.create({
domain: testDomain._id,
userId: id("no-member"),
email: email("no-member"),
Expand Down Expand Up @@ -313,7 +330,7 @@ describe("createCommunityPost", () => {
ctx: regularCtx,
});

const dbPost = await CommunityPostModel.findOne({
const dbPost = await communityPostRepo.findOne({
domain: testDomain._id,
postId: result.postId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ import UserModel from "@models/User";
import CommunityPostSubscriberModel from "@models/CommunityPostSubscriber";
import constants from "@/config/constants";
import { Constants } from "@courselit/common-models";
import {
CommunityPostRepository,
CommunityCommentRepository,
UserRepository,
MembershipRepository,
PaymentPlanRepository,
PageRepository,
CommunityRepository,
DomainRepository,
} from "@courselit/orm-models";

const communityCommentRepo = new CommunityCommentRepository(
CommunityCommentModel,
);
const communityPostRepo = new CommunityPostRepository(CommunityPostModel);
const communityRepo = new CommunityRepository(CommunityModel);
const domainRepo = new DomainRepository(DomainModel);
const membershipRepo = new MembershipRepository(MembershipModel);
const pageRepo = new PageRepository(PageModel);
const paymentPlanRepo = new PaymentPlanRepository(PaymentPlanModel);
const userRepo = new UserRepository(UserModel);

jest.mock("@/services/medialit");
jest.mock("@/services/queue");
Expand All @@ -40,12 +61,12 @@ describe("deleteCommunityPost", () => {
let existingPost: any;

beforeAll(async () => {
testDomain = await DomainModel.create({
testDomain = await domainRepo.create({
name: id("domain"),
email: email("domain"),
});

adminUser = await UserModel.create({
adminUser = await userRepo.create({
domain: testDomain._id,
userId: id("admin"),
email: email("admin"),
Expand All @@ -55,7 +76,7 @@ describe("deleteCommunityPost", () => {
unsubscribeToken: id("unsub-admin"),
});

regularUser = await UserModel.create({
regularUser = await userRepo.create({
domain: testDomain._id,
userId: id("regular"),
email: email("regular"),
Expand All @@ -65,7 +86,7 @@ describe("deleteCommunityPost", () => {
unsubscribeToken: id("unsub-regular"),
});

otherMemberUser = await UserModel.create({
otherMemberUser = await userRepo.create({
domain: testDomain._id,
userId: id("other"),
email: email("other"),
Expand All @@ -76,7 +97,7 @@ describe("deleteCommunityPost", () => {
});

// Internal payment plan (required by the system)
await PaymentPlanModel.create({
await paymentPlanRepo.create({
domain: testDomain._id,
planId: id("internal-plan"),
userId: adminUser.userId,
Expand All @@ -90,7 +111,7 @@ describe("deleteCommunityPost", () => {
currencyISOCode: "USD",
});

community = await CommunityModel.create({
community = await communityRepo.create({
domain: testDomain._id,
communityId: id("community"),
name: "Test Community",
Expand All @@ -101,7 +122,7 @@ describe("deleteCommunityPost", () => {
categories: ["General", "Announcements"],
});

await PageModel.create({
await pageRepo.create({
domain: testDomain._id,
pageId: community.pageId,
type: constants.communityPage,
Expand All @@ -111,7 +132,7 @@ describe("deleteCommunityPost", () => {
});

// Free payment plan for the community
await PaymentPlanModel.create({
await paymentPlanRepo.create({
domain: testDomain._id,
planId: id("free-plan"),
userId: adminUser.userId,
Expand All @@ -125,7 +146,7 @@ describe("deleteCommunityPost", () => {
});

// Admin membership (MODERATE role)
await MembershipModel.create({
await membershipRepo.create({
domain: testDomain._id,
membershipId: id("admin-membership"),
userId: adminUser.userId,
Expand All @@ -138,7 +159,7 @@ describe("deleteCommunityPost", () => {
});

// Regular user membership (POST role)
await MembershipModel.create({
await membershipRepo.create({
domain: testDomain._id,
membershipId: id("regular-membership"),
userId: regularUser.userId,
Expand All @@ -151,7 +172,7 @@ describe("deleteCommunityPost", () => {
});

// Other member membership (POST role)
await MembershipModel.create({
await membershipRepo.create({
domain: testDomain._id,
membershipId: id("other-membership"),
userId: otherMemberUser.userId,
Expand Down Expand Up @@ -218,7 +239,7 @@ describe("deleteCommunityPost", () => {
});

it("should throw action_not_allowed if missing membership", async () => {
const noMemberUser = await UserModel.create({
const noMemberUser = await userRepo.create({
domain: testDomain._id,
userId: id("no-member-del"),
email: email("no-member-del"),
Expand Down Expand Up @@ -263,7 +284,7 @@ describe("deleteCommunityPost", () => {
ctx: regularCtx,
});

const dbPost = await CommunityPostModel.findOne({
const dbPost = await communityPostRepo.findOne({
postId: existingPost.postId,
});
expect(dbPost).toBeNull();
Expand All @@ -276,14 +297,14 @@ describe("deleteCommunityPost", () => {
ctx: adminCtx,
});

const dbPost = await CommunityPostModel.findOne({
const dbPost = await communityPostRepo.findOne({
postId: existingPost.postId,
});
expect(dbPost).toBeNull();
});

it("should delete associated comments", async () => {
await CommunityCommentModel.create({
await communityCommentRepo.create({
domain: testDomain._id,
communityId: community.communityId,
postId: existingPost.postId,
Expand All @@ -298,7 +319,7 @@ describe("deleteCommunityPost", () => {
ctx: regularCtx,
});

const dbComments = await CommunityCommentModel.find({
const dbComments = await communityCommentRepo.find({
postId: existingPost.postId,
});
expect(dbComments.length).toBe(0);
Expand Down Expand Up @@ -349,7 +370,7 @@ describe("deleteCommunityPost", () => {
);

// Post should be deleted regardless of deleteMedia failure
const dbPost = await CommunityPostModel.findOne({
const dbPost = await communityPostRepo.findOne({
postId: existingPost.postId,
});
expect(dbPost).toBeNull();
Expand Down Expand Up @@ -391,7 +412,7 @@ describe("deleteCommunityPost", () => {

expect(medialit.deleteMedia).toHaveBeenCalledWith("media-success");

const dbPost = await CommunityPostModel.findOne({
const dbPost = await communityPostRepo.findOne({
postId: existingPost.postId,
});
expect(dbPost).toBeNull();
Expand Down
Loading
Loading