Skip to content

Commit

Permalink
Merge branch 'main' into rajat1saxena/issue487
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat Saxena committed Nov 14, 2024
2 parents 3adf235 + fbca787 commit f2545ab
Show file tree
Hide file tree
Showing 35 changed files with 149 additions and 132 deletions.
12 changes: 12 additions & 0 deletions .yarn/versions/637351c1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
undecided:
- "@courselit/docs"
- "@courselit/queue"
- "@courselit/web"
- "@courselit/common-models"
- "@courselit/common-widgets"
- "@courselit/components-library"
- "@courselit/icons"
- "@courselit/state-management"
- tailwind-config
- "@courselit/text-editor"
- tsconfig
12 changes: 12 additions & 0 deletions .yarn/versions/ba63f441.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
undecided:
- "@courselit/docs"
- "@courselit/queue"
- "@courselit/web"
- "@courselit/common-models"
- "@courselit/common-widgets"
- "@courselit/components-library"
- "@courselit/icons"
- "@courselit/state-management"
- tailwind-config
- "@courselit/text-editor"
- tsconfig
2 changes: 1 addition & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@courselit/docs",
"type": "module",
"version": "0.31.5",
"version": "0.31.7",
"private": true,
"scripts": {
"dev": "astro dev",
Expand Down
2 changes: 1 addition & 1 deletion apps/queue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@courselit/queue",
"version": "0.22.5",
"version": "0.22.7",
"private": true,
"packageManager": "[email protected]",
"scripts": {
Expand Down
5 changes: 2 additions & 3 deletions apps/web/app/dashboard2/blogs/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export async function generateMetadata(
parent: ResolvingMetadata,
): Promise<Metadata> {
return {
title: `${MANAGE_BLOG_PAGE_HEADING} | ${
(await parent)?.title?.absolute
}`,
title: `${MANAGE_BLOG_PAGE_HEADING} | ${(await parent)?.title
?.absolute}`,
};
}

Expand Down
5 changes: 2 additions & 3 deletions apps/web/app/dashboard2/pages/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export async function generateMetadata(
parent: ResolvingMetadata,
): Promise<Metadata> {
return {
title: `${MANAGE_PAGES_PAGE_HEADING} | ${
(await parent)?.title?.absolute
}`,
title: `${MANAGE_PAGES_PAGE_HEADING} | ${(await parent)?.title
?.absolute}`,
};
}

Expand Down
5 changes: 2 additions & 3 deletions apps/web/app/dashboard2/products/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export async function generateMetadata(
parent: ResolvingMetadata,
): Promise<Metadata> {
return {
title: `${MANAGE_COURSES_PAGE_HEADING} | ${
(await parent)?.title?.absolute
}`,
title: `${MANAGE_COURSES_PAGE_HEADING} | ${(await parent)?.title
?.absolute}`,
};
}

Expand Down
5 changes: 2 additions & 3 deletions apps/web/app/dashboard2/settings/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export async function generateMetadata(
parent: ResolvingMetadata,
): Promise<Metadata> {
return {
title: `${SITE_SETTINGS_PAGE_HEADING} | ${
(await parent)?.title?.absolute
}`,
title: `${SITE_SETTINGS_PAGE_HEADING} | ${(await parent)?.title
?.absolute}`,
};
}

Expand Down
6 changes: 3 additions & 3 deletions apps/web/app/verify-domain/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export async function GET(req: Request) {
if (!domain) {
return Response.json(
{
message: `${responses.domain_doesnt_exist}: ${
host?.split(".")[0]
}`,
message: `${responses.domain_doesnt_exist}: ${host?.split(
".",
)[0]}`,
},
{ status: 404 },
);
Expand Down
3 changes: 3 additions & 0 deletions apps/web/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const { auth, signIn, signOut, handlers } = NextAuth({
email: sanitizedEmail,
});
}
if (!user.active) {
return null;
}
return {
id: user.userId,
email: sanitizedEmail,
Expand Down
10 changes: 5 additions & 5 deletions apps/web/components/admin/mails/sequence-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,11 @@ const SequenceEditor = ({
value: tag.tag,
}))
: triggerType === "PRODUCT_PURCHASED"
? products.map((product) => ({
label: product.title,
value: product.courseId,
}))
: []
? products.map((product) => ({
label: product.title,
value: product.courseId,
}))
: []
}
/>
)}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/components/admin/page-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ export default function PageEditor({
typeof page.draftRobotsAllowed === "boolean"
? page.draftRobotsAllowed
: typeof page.robotsAllowed === "boolean"
? page.robotsAllowed
: true
? page.robotsAllowed
: true
}
socialImage={page.draftSocialImage || {}}
onClose={(e) => setLeftPaneContent("none")}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/components/admin/users/details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const Details = ({ userId, address, dispatch }: DetailsProps) => {
/>
</div>
</Section>
<PermissionsEditor user={userData} />
{userData.permissions && <PermissionsEditor user={userData} />}
</div>

{/*
Expand Down
5 changes: 5 additions & 0 deletions apps/web/graphql/mails/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import pug from "pug";
import digitalDownloadTemplate from "../../templates/download-link";
import { send } from "../../services/mail";
import { responses } from "@config/strings";
import { generateEmailFrom } from "@/lib/utils";

export function areAllEmailIdsValid(
emailsOrder: string[],
Expand Down Expand Up @@ -95,6 +96,10 @@ export async function createTemplateAndSendMail({
to: [user.email],
subject: `Thank you for signing up for ${course.title}`,
body: emailBody,
from: generateEmailFrom({
name: ctx.subdomain?.settings?.title || ctx.subdomain.name,
email: process.env.EMAIL_FROM || ctx.subdomain.email,
}),
});
}

Expand Down
4 changes: 2 additions & 2 deletions apps/web/graphql/settings/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
UIConstants,
} from "@courselit/common-models";

const currencyISOCodes = currencies.map((currency) =>
currency.isoCode?.toLowerCase(),
const currencyISOCodes = currencies.map(
(currency) => currency.isoCode?.toLowerCase(),
);

const verifyCurrencyISOCode = (isoCode: string) => {
Expand Down
117 changes: 48 additions & 69 deletions apps/web/graphql/users/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,37 @@ import { getCourseOrThrow } from "../courses/logic";
import pug from "pug";
import courseEnrollTemplate from "@/templates/course-enroll";
import { send } from "../../services/mail";

const removeAdminFieldsFromUserObject = ({
id,
name,
userId,
bio,
email,
avatar,
}: {
id: string;
name: string;
userId: string;
bio: string;
email: string;
avatar: Media;
}) => ({
id,
name,
userId,
bio,
email,
avatar,
import { generateEmailFrom } from "@/lib/utils";

const removeAdminFieldsFromUserObject = (user: User) => ({
id: user._id,
name: user.name,
userId: user.userId,
bio: user.bio,
email: user.email,
avatar: user.avatar,
});

export const getUser = async (email = null, userId = null, ctx: GQLContext) => {
const { user: loggedInUser } = ctx;
const loggedUserEmail = loggedInUser && loggedInUser.email;
const loggedUserId = loggedInUser && loggedInUser.userId;
export const getUser = async (userId = null, ctx: GQLContext) => {
let user: User | undefined | null;
user = ctx.user;

if (!email && !userId && !loggedInUser) {
throw new Error(responses.invalid_user_id);
}

if (!email && !userId && loggedInUser) {
email = loggedUserEmail;
}

let user;
if (email) {
user = await UserModel.findOne({ email, domain: ctx.subdomain._id });
} else {
if (userId) {
user = await UserModel.findOne({ userId, domain: ctx.subdomain._id });
}

if (!user) {
throw new Error(responses.item_not_found);
}

user.userId = user.userId || -1; // Set -1 for empty userIds; Backward compatibility;

return loggedInUser &&
(loggedUserEmail === email ||
loggedUserId === userId ||
checkPermission(loggedInUser.permissions, [
permissions.manageUsers,
]))
? user
: removeAdminFieldsFromUserObject(user);
if (
user.userId === ctx.user.userId ||
checkPermission(ctx.user.permissions, [permissions.manageUsers])
) {
return user;
} else {
return removeAdminFieldsFromUserObject(user);
}
};

const validateUserProperties = (user) => {
Expand All @@ -102,39 +75,41 @@ const checkForInvalidPermissions = (user) => {
}
};

export const updateUser = async (
userData: Record<string, unknown>,
ctx: GQLContext,
) => {
interface UserData {
id: string;
name?: string;
active?: boolean;
bio?: string;
permissions?: string[];
subscribedToUpdates?: boolean;
tags?: string[];
avatar?: Media;
}

export const updateUser = async (userData: UserData, ctx: GQLContext) => {
checkIfAuthenticated(ctx);
const { id } = userData;
const keys = Object.keys(userData);

const hasPermissionToManageUser = checkPermission(ctx.user.permissions, [
permissions.manageUsers,
]);
if (!hasPermissionToManageUser) {
if (id !== ctx.user._id.toString()) {
throw new Error(responses.action_not_allowed);
}
const isModifyingSelf = id === ctx.user._id.toString();
const restrictedKeys = ["permissions", "active"];

if (
(isModifyingSelf && keys.some((key) => restrictedKeys.includes(key))) ||
(!isModifyingSelf && !hasPermissionToManageUser)
) {
throw new Error(responses.action_not_allowed);
}

let user = await UserModel.findOne({ _id: id, domain: ctx.subdomain._id });
if (!user) throw new Error(responses.item_not_found);

for (const key of Object.keys(userData)) {
if (key === "id") {
continue;
}

// if (
// !["subscribedToUpdates"].includes(key) &&
// id === ctx.user._id.toString()
// ) {
// throw new Error(responses.action_not_allowed);
// }

for (const key of keys.filter((key) => key !== "id")) {
if (key === "tags") {
addTags(userData["tags"], ctx);
addTags(userData["tags"]!, ctx);
}

user[key] = userData[key];
Expand Down Expand Up @@ -207,6 +182,10 @@ export const inviteCustomer = async (
to: [user.email],
subject: `You have been invited to ${course.title}`,
body: emailBody,
from: generateEmailFrom({
name: ctx.subdomain?.settings?.title || ctx.subdomain.name,
email: process.env.EMAIL_FROM || ctx.subdomain.email,
}),
});
} catch (error) {
// eslint-disable-next-line no-console
Expand Down
4 changes: 2 additions & 2 deletions apps/web/graphql/users/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const queries = {
email: { type: GraphQLString },
userId: { type: GraphQLString },
},
resolve: (_: any, { email, userId }: any, context: GQLContext) =>
getUser(email, userId, context),
resolve: (_: any, { userId }: any, context: GQLContext) =>
getUser(userId, context),
},
getUsers: {
type: new GraphQLList(types.userType),
Expand Down
2 changes: 1 addition & 1 deletion apps/web/graphql/users/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const userType = new GraphQLObjectType({
email: { type: new GraphQLNonNull(GraphQLString) },
name: { type: GraphQLString },
purchases: { type: new GraphQLList(progress) },
active: { type: new GraphQLNonNull(GraphQLBoolean) },
active: { type: GraphQLBoolean },
userId: { type: new GraphQLNonNull(GraphQLString) },
bio: { type: GraphQLString },
permissions: { type: new GraphQLList(GraphQLString) },
Expand Down
10 changes: 10 additions & 0 deletions apps/web/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ export const getAddress = (
export const getProtocol = (protocol: string | string[] = "http") => {
return protocol.includes("https") ? "https" : "http";
};

export const generateEmailFrom = ({
name,
email,
}: {
name: string;
email: string;
}) => {
return `${name} <${email}>`;
};
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@courselit/web",
"version": "0.54.5",
"version": "0.54.7",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
Loading

0 comments on commit f2545ab

Please sign in to comment.