add member onboarding
This commit is contained in:
@@ -12,6 +12,11 @@ export default async function getUserById(userId: number) {
|
||||
},
|
||||
},
|
||||
subscriptions: true,
|
||||
parentSubscription: {
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -22,7 +27,8 @@ export default async function getUserById(userId: number) {
|
||||
(usernames) => usernames.username
|
||||
);
|
||||
|
||||
const { password, subscriptions, ...lessSensitiveInfo } = user;
|
||||
const { password, subscriptions, parentSubscription, ...lessSensitiveInfo } =
|
||||
user;
|
||||
|
||||
const data = {
|
||||
...lessSensitiveInfo,
|
||||
@@ -30,6 +36,12 @@ export default async function getUserById(userId: number) {
|
||||
subscription: {
|
||||
active: subscriptions?.active,
|
||||
},
|
||||
parentSubscription: {
|
||||
active: parentSubscription?.active,
|
||||
user: {
|
||||
email: parentSubscription?.user.email,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return { response: data, status: 200 };
|
||||
|
||||
@@ -101,7 +101,6 @@ export default async function updateUserById(
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { email: true, password: true, name: true },
|
||||
});
|
||||
|
||||
if (user && user.email && data.email && data.email !== user.email) {
|
||||
@@ -170,8 +169,20 @@ export default async function updateUserById(
|
||||
|
||||
// Other settings / Apply changes
|
||||
|
||||
const isInvited =
|
||||
user?.name === null && user.parentSubscriptionId && !user.password;
|
||||
|
||||
if (isInvited && data.password === "")
|
||||
return {
|
||||
response: "Password is required.",
|
||||
status: 400,
|
||||
};
|
||||
|
||||
const saltRounds = 10;
|
||||
const newHashedPassword = bcrypt.hashSync(data.newPassword || "", saltRounds);
|
||||
const newHashedPassword = bcrypt.hashSync(
|
||||
data.newPassword || data.password || "",
|
||||
saltRounds
|
||||
);
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: {
|
||||
@@ -198,18 +209,28 @@ export default async function updateUserById(
|
||||
linksRouteTo: data.linksRouteTo,
|
||||
preventDuplicateLinks: data.preventDuplicateLinks,
|
||||
password:
|
||||
data.newPassword && data.newPassword !== ""
|
||||
isInvited || (data.newPassword && data.newPassword !== "")
|
||||
? newHashedPassword
|
||||
: undefined,
|
||||
},
|
||||
include: {
|
||||
whitelistedUsers: true,
|
||||
subscriptions: true,
|
||||
parentSubscription: {
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const { whitelistedUsers, password, subscriptions, ...userInfo } =
|
||||
updatedUser;
|
||||
const {
|
||||
whitelistedUsers,
|
||||
password,
|
||||
subscriptions,
|
||||
parentSubscription,
|
||||
...userInfo
|
||||
} = updatedUser;
|
||||
|
||||
// If user.whitelistedUsers is not provided, we will assume the whitelistedUsers should be removed
|
||||
const newWhitelistedUsernames: string[] = data.whitelistedUsers || [];
|
||||
@@ -250,11 +271,19 @@ export default async function updateUserById(
|
||||
});
|
||||
}
|
||||
|
||||
const response: Omit<AccountSettings, "password"> = {
|
||||
const response = {
|
||||
...userInfo,
|
||||
whitelistedUsers: newWhitelistedUsernames,
|
||||
image: userInfo.image ? `${userInfo.image}?${Date.now()}` : "",
|
||||
subscription: { active: subscriptions?.active },
|
||||
subscription: {
|
||||
active: subscriptions?.active,
|
||||
},
|
||||
parentSubscription: {
|
||||
active: parentSubscription?.active,
|
||||
user: {
|
||||
email: parentSubscription?.user.email,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return { response, status: 200 };
|
||||
|
||||
@@ -17,6 +17,7 @@ export default async function paymentCheckout(
|
||||
},
|
||||
include: {
|
||||
subscriptions: true,
|
||||
parentSubscription: true,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -4,17 +4,22 @@ import checkSubscriptionByEmail from "./checkSubscriptionByEmail";
|
||||
|
||||
interface UserIncludingSubscription extends User {
|
||||
subscriptions: Subscription | null;
|
||||
parentSubscription: Subscription | null;
|
||||
}
|
||||
|
||||
export default async function verifySubscription(
|
||||
user?: UserIncludingSubscription | null
|
||||
) {
|
||||
if (!user || !user.subscriptions) {
|
||||
if (!user || (!user.subscriptions && !user.parentSubscription)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (user.parentSubscription?.active) {
|
||||
return user;
|
||||
}
|
||||
|
||||
if (
|
||||
!user.subscriptions.active ||
|
||||
!user.subscriptions?.active ||
|
||||
new Date() > user.subscriptions.currentPeriodEnd
|
||||
) {
|
||||
const subscription = await checkSubscriptionByEmail(user.email as string);
|
||||
|
||||
@@ -33,6 +33,7 @@ export default async function verifyByCredentials({
|
||||
},
|
||||
include: {
|
||||
subscriptions: true,
|
||||
parentSubscription: true,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ export default async function verifyUser({
|
||||
},
|
||||
include: {
|
||||
subscriptions: true,
|
||||
parentSubscription: true,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user