Added propagate status changes
This commit is contained in:
@@ -12,6 +12,8 @@ import moment from "moment";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import {Payment} from "@/interfaces/paypal";
|
||||
import {toFixedNumber} from "@/utils/number";
|
||||
import { propagateStatusChange } from '@/utils/propagate.user.changes';
|
||||
|
||||
const db = getFirestore(app);
|
||||
const auth = getAuth(app);
|
||||
|
||||
@@ -74,12 +76,20 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const userRef = doc(db, "users", req.query.id ? (req.query.id as string) : req.session.user.id);
|
||||
const queryId = req.query.id as string;
|
||||
|
||||
const userRef = doc(db, "users", queryId ? (queryId as string) : req.session.user.id);
|
||||
const updatedUser = req.body as User & {password?: string; newPassword?: string};
|
||||
|
||||
if (!!req.query.id) {
|
||||
if (!!queryId) {
|
||||
const user = await setDoc(userRef, updatedUser, {merge: true});
|
||||
await managePaymentRecords(updatedUser, updatedUser.id);
|
||||
|
||||
if(updatedUser.status) {
|
||||
// there's no await as this does not affect the user
|
||||
propagateStatusChange(queryId, updatedUser.status);
|
||||
}
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
return;
|
||||
}
|
||||
@@ -132,6 +142,11 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
}
|
||||
|
||||
if(updatedUser.status) {
|
||||
// there's no await as this does not affect the user
|
||||
propagateStatusChange(req.session.user.id, updatedUser.status);
|
||||
}
|
||||
|
||||
delete updatedUser.password;
|
||||
delete updatedUser.newPassword;
|
||||
|
||||
@@ -140,12 +155,12 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
||||
const user = docUser.data() as User;
|
||||
|
||||
if (!req.query.id) {
|
||||
if (!queryId) {
|
||||
req.session.user = {...user, id: req.session.user.id};
|
||||
await req.session.save();
|
||||
}
|
||||
|
||||
await managePaymentRecords(user, req.query.id);
|
||||
await managePaymentRecords(user, queryId);
|
||||
|
||||
res.status(200).json({user});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user