mirror of
https://github.com/formbricks/formbricks.git
synced 2026-02-25 10:20:03 -06:00
fix: fixes notification subscription based on user membership (#2779)
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
OperationNotAllowedError,
|
||||
ResourceNotFoundError,
|
||||
} from "@formbricks/types/errors";
|
||||
import { TUserNotificationSettings } from "@formbricks/types/user";
|
||||
|
||||
export const createShortUrlAction = async (url: string) => {
|
||||
const session = await getServerSession(authOptions);
|
||||
@@ -54,7 +55,7 @@ export const createOrganizationAction = async (organizationName: string): Promis
|
||||
name: "My Product",
|
||||
});
|
||||
|
||||
const updatedNotificationSettings = {
|
||||
const updatedNotificationSettings: TUserNotificationSettings = {
|
||||
...session.user.notificationSettings,
|
||||
alert: {
|
||||
...session.user.notificationSettings?.alert,
|
||||
@@ -63,6 +64,9 @@ export const createOrganizationAction = async (organizationName: string): Promis
|
||||
...session.user.notificationSettings?.weeklySummary,
|
||||
[product.id]: true,
|
||||
},
|
||||
unsubscribedOrganizationIds: Array.from(
|
||||
new Set([...(session.user.notificationSettings?.unsubscribedOrganizationIds || []), newOrganization.id])
|
||||
),
|
||||
};
|
||||
|
||||
await updateUser(session.user.id, {
|
||||
|
||||
@@ -85,7 +85,18 @@ const Page = async ({ searchParams }) => {
|
||||
session.user?.name ?? "",
|
||||
invite.creator.email
|
||||
);
|
||||
await updateUser(session.user.id, { onboardingCompleted: true });
|
||||
await updateUser(session.user.id, {
|
||||
onboardingCompleted: true,
|
||||
notificationSettings: {
|
||||
...session.user.notificationSettings,
|
||||
unsubscribedOrganizationIds: Array.from(
|
||||
new Set([
|
||||
...(session.user.notificationSettings?.unsubscribedOrganizationIds || []),
|
||||
invite.organizationId,
|
||||
])
|
||||
),
|
||||
},
|
||||
});
|
||||
return (
|
||||
<ContentLayout headline="You’re in 🎉" description="Welcome to the organization.">
|
||||
<Button variant="darkCTA" href="/">
|
||||
|
||||
@@ -68,6 +68,14 @@ export const POST = async (request: Request) => {
|
||||
role: invite.role,
|
||||
});
|
||||
|
||||
await updateUser(user.id, {
|
||||
notificationSettings: {
|
||||
alert: {},
|
||||
weeklySummary: {},
|
||||
unsubscribedOrganizationIds: [invite.organizationId],
|
||||
},
|
||||
});
|
||||
|
||||
if (!EMAIL_VERIFICATION_DISABLED) {
|
||||
await sendVerificationEmail(user);
|
||||
}
|
||||
@@ -94,6 +102,16 @@ export const POST = async (request: Request) => {
|
||||
}
|
||||
const role = isNewOrganization ? "owner" : DEFAULT_ORGANIZATION_ROLE || "admin";
|
||||
await createMembership(organization.id, user.id, { role, accepted: true });
|
||||
const updatedNotificationSettings = {
|
||||
...user.notificationSettings,
|
||||
unsubscribedOrganizationIds: Array.from(
|
||||
new Set([...(user.notificationSettings?.unsubscribedOrganizationIds || []), organization.id])
|
||||
),
|
||||
};
|
||||
|
||||
await updateUser(user.id, {
|
||||
notificationSettings: updatedNotificationSettings,
|
||||
});
|
||||
}
|
||||
// Without default organization assignment
|
||||
else {
|
||||
@@ -114,6 +132,9 @@ export const POST = async (request: Request) => {
|
||||
...user.notificationSettings?.weeklySummary,
|
||||
[product.id]: true,
|
||||
},
|
||||
unsubscribedOrganizationIds: Array.from(
|
||||
new Set([...(user.notificationSettings?.unsubscribedOrganizationIds || []), organization.id])
|
||||
),
|
||||
};
|
||||
|
||||
await updateUser(user.id, {
|
||||
|
||||
@@ -44,6 +44,9 @@ export const createOrganizationAction = async (organizationName: string): Promis
|
||||
...session.user.notificationSettings?.weeklySummary,
|
||||
[product.id]: true,
|
||||
},
|
||||
unsubscribedOrganizationIds: Array.from(
|
||||
new Set([...(session.user.notificationSettings?.unsubscribedOrganizationIds || []), newOrganization.id])
|
||||
),
|
||||
};
|
||||
|
||||
await updateUser(session.user.id, {
|
||||
|
||||
@@ -275,6 +275,23 @@ export const authOptions: NextAuthOptions = {
|
||||
...account,
|
||||
userId: userProfile.id,
|
||||
});
|
||||
|
||||
const updatedNotificationSettings = {
|
||||
...userProfile.notificationSettings,
|
||||
alert: {
|
||||
...userProfile.notificationSettings?.alert,
|
||||
},
|
||||
unsubscribedOrganizationIds: Array.from(
|
||||
new Set([
|
||||
...(userProfile.notificationSettings?.unsubscribedOrganizationIds || []),
|
||||
organization.id,
|
||||
])
|
||||
),
|
||||
};
|
||||
|
||||
await updateUser(userProfile.id, {
|
||||
notificationSettings: updatedNotificationSettings,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
// Without default organization assignment
|
||||
|
||||
@@ -15,7 +15,7 @@ import { cache } from "../cache";
|
||||
import { BILLING_LIMITS, ITEMS_PER_PAGE, PRODUCT_FEATURE_KEYS } from "../constants";
|
||||
import { environmentCache } from "../environment/cache";
|
||||
import { getProducts } from "../product/service";
|
||||
import { getUsersWithOrganization, updateUser } from "../user/service";
|
||||
import { updateUser } from "../user/service";
|
||||
import { validateInputs } from "../utils/validate";
|
||||
import { organizationCache } from "./cache";
|
||||
|
||||
@@ -346,35 +346,31 @@ export const getMonthlyOrganizationResponseCount = (organizationId: string): Pro
|
||||
)();
|
||||
|
||||
export const subscribeOrganizationMembersToSurveyResponses = async (
|
||||
environmentId: string,
|
||||
surveyId: string
|
||||
surveyId: string,
|
||||
createdBy: string
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const organization = await getOrganizationByEnvironmentId(environmentId);
|
||||
if (!organization) {
|
||||
throw new ResourceNotFoundError("Organization", environmentId);
|
||||
const surveyCreator = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: createdBy,
|
||||
},
|
||||
});
|
||||
|
||||
if (!surveyCreator) {
|
||||
throw new ResourceNotFoundError("User", createdBy);
|
||||
}
|
||||
|
||||
const users = await getUsersWithOrganization(organization.id);
|
||||
await Promise.all(
|
||||
users.map((user) => {
|
||||
if (!user.notificationSettings?.unsubscribedOrganizationIds?.includes(organization?.id as string)) {
|
||||
const defaultSettings = { alert: {}, weeklySummary: {} };
|
||||
const updatedNotificationSettings: TUserNotificationSettings = {
|
||||
...defaultSettings,
|
||||
...user.notificationSettings,
|
||||
};
|
||||
const defaultSettings = { alert: {}, weeklySummary: {} };
|
||||
const updatedNotificationSettings: TUserNotificationSettings = {
|
||||
...defaultSettings,
|
||||
...surveyCreator.notificationSettings,
|
||||
};
|
||||
|
||||
updatedNotificationSettings.alert[surveyId] = true;
|
||||
updatedNotificationSettings.alert[surveyId] = true;
|
||||
|
||||
return updateUser(user.id, {
|
||||
notificationSettings: updatedNotificationSettings,
|
||||
});
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
})
|
||||
);
|
||||
await updateUser(surveyCreator.id, {
|
||||
notificationSettings: updatedNotificationSettings,
|
||||
});
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -662,13 +662,15 @@ export const createSurvey = async (environmentId: string, surveyBody: TSurveyInp
|
||||
}),
|
||||
};
|
||||
|
||||
await subscribeOrganizationMembersToSurveyResponses(environmentId, survey.id);
|
||||
|
||||
surveyCache.revalidate({
|
||||
id: survey.id,
|
||||
environmentId: survey.environmentId,
|
||||
});
|
||||
|
||||
if (createdBy) {
|
||||
await subscribeOrganizationMembersToSurveyResponses(survey.id, createdBy);
|
||||
}
|
||||
|
||||
return transformedSurvey;
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
|
||||
Reference in New Issue
Block a user