Skip to content

Commit

Permalink
feat: added tanstack query hook
Browse files Browse the repository at this point in the history
  • Loading branch information
gmat224 committed Dec 21, 2024
1 parent c95f677 commit a030c39
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 137 deletions.
3 changes: 2 additions & 1 deletion api/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { insertUserBySuperToken } from "../gateway/userGateway";

export const updateUserTicketInfo = asyncHandler(
async (req: Request, res: Response) => {
console.log(process.env.DOMAIN_DB);
// console.log(process.env.DOMAIN_DB);
console.log(req.body)
try {
const { name, email, phoneNumber, ticketId, answers } = req.body;

Expand Down
168 changes: 85 additions & 83 deletions api/gateway/userGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,22 @@ export async function insertUserTicket(data: {

//figure out how to add People_ID link to userTickets
//find people id by getting the email? but it will fail if the user isn't a member
let people = await db
.select()
.from(peoples)
.where(eq(peoples.email, data.email))
.limit(1);

const userTicketIdLink = await db
.insert(userTicketsTicketIdLinks)
.values({
userTicketId: newUserTicket[0].userTicketId,
ticketId: data.ticketId,
})
.returning();

console.log("insertUserTicket: userTicketIdLink: " + userTicketIdLink[0]);
// todo: I think this query fails if there is no one with that email in the db, and probs adding a catch would fix the issue
// let people = await db
// .select()
// .from(peoples)
// .where(eq(peoples.email, data.email))
// .limit(1);

// const userTicketIdLink = await db
// .insert(userTicketsTicketIdLinks)
// .values({
// userTicketId: newUserTicket[0].userTicketId,
// ticketId: data.ticketId,
// })
// .returning();

// console.log("insertUserTicket: userTicketIdLink: " + userTicketIdLink[0]);

const ticketId = newUserTicket[0].userTicketId;

Expand Down Expand Up @@ -182,23 +183,24 @@ export async function insertUserTicket(data: {
"insertUserTicket: userTicketsPeopleIdLink: ticketId: ",
data.ticketId
);
let userTicketsPeopleIdLink = await db
.insert(userTicketsPeopleIdLinks)
.values({
peopleId: people[0].id,
userTicketId: ticketId,
})
.returning()
.catch((error) => {
console.log(
"insertUserTicket: userTicketsPeopleIdLink: error occurred: ",
error
);
});

console.log(
"insertUserTicket: userTicketsPeopleIdLink: " + userTicketsPeopleIdLink![0]!
);

// let userTicketsPeopleIdLink = await db
// .insert(userTicketsPeopleIdLinks)
// .values({
// peopleId: people[0].id,
// userTicketId: ticketId,
// })
// .returning()
// .catch((error) => {
// console.log(
// "insertUserTicket: userTicketsPeopleIdLink: error occurred: ",
// error
// );
// });

// console.log(
// "insertUserTicket: userTicketsPeopleIdLink: " + userTicketsPeopleIdLink![0]!
// );

//for question: link the answer.id to this question. More of an admin type task.
//let question = await db.update(questions).set().where(eq(questions.id, data.answers[0].questionId));
Expand All @@ -222,57 +224,57 @@ export async function updateUserMembershipExpiryDate(
expand: ["line_items"],
});

try {
//since this is for memberships, get the current user by their email id
let customer = await db
.select()
.from(peoples)
.where(eq(peoples.email, checkoutSession.customer_details!.email!))
.limit(1);

console.log("updateUserMembershipExpiryDate: customer: " + customer);

//then, retrieve the price id from metadata from purchaseableMemberships
let expiryDate = await db
.select()
.from(purchasableMemberships)
.where(
eq(
purchasableMemberships.stripeLink,
checkoutSession.metadata!["priceId"]
)
)
.limit(1);

console.log("updateUserMembershipExpiryDate: expiryDate: " + expiryDate);

// then, apply the retrieved expiry date into the users' field
let updateExpiryDate = await db
.update(peoples)
.set({ memberExpiryDate: expiryDate[0].expiry, isMember: true })
.where(eq(peoples.email, checkoutSession.customer_details!.email!))
.returning({ memberExpiryDate: peoples.memberExpiryDate });

console.log(
"updateUserMembershipExpiryDate: updateExpiryDate: " + updateExpiryDate
);

//update user metadata
//getUserIdByEmail
let customerEmail = await getUserEmail(
checkoutSession.customer_details!.email!
);

let userId = await getUserIdByEmail(customerEmail);

await updateUserMetadata(userId, {
bIsMembershipPaymentComplete: true,
});
} catch (error) {
throw new Error(
"Unknown error occurred while trying to update user membership: " + error
);
}
// try {
// //since this is for memberships, get the current user by their email id
// let customer = await db
// .select()
// .from(peoples)
// .where(eq(peoples.email, checkoutSession.customer_details!.email!))
// .limit(1);

// console.log("updateUserMembershipExpiryDate: customer: " + customer);

// //then, retrieve the price id from metadata from purchaseableMemberships
// let expiryDate = await db
// .select()
// .from(purchasableMemberships)
// .where(
// eq(
// purchasableMemberships.stripeLink,
// checkoutSession.metadata!["priceId"]
// )
// )
// .limit(1);

// console.log("updateUserMembershipExpiryDate: expiryDate: " + expiryDate);

// // then, apply the retrieved expiry date into the users' field
// let updateExpiryDate = await db
// .update(peoples)
// .set({ memberExpiryDate: expiryDate[0].expiry, isMember: true })
// .where(eq(peoples.email, checkoutSession.customer_details!.email!))
// .returning({ memberExpiryDate: peoples.memberExpiryDate });

// console.log(
// "updateUserMembershipExpiryDate: updateExpiryDate: " + updateExpiryDate
// );

// //update user metadata
// //getUserIdByEmail
// let customerEmail = await getUserEmail(
// checkoutSession.customer_details!.email!
// );

// let userId = await getUserIdByEmail(customerEmail);

// await updateUserMetadata(userId, {
// bIsMembershipPaymentComplete: true,
// });
// } catch (error) {
// throw new Error(
// "Unknown error occurred while trying to update user membership: " + error
// );
// }
}

export async function insertUserBySuperToken(
Expand Down
45 changes: 35 additions & 10 deletions web/src/api/apiRequests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import axios, { AxiosResponse } from "axios";
import {
AnswerList,
AttendanceList,
MembershipExpiryDate as MembershipExpiryDate,
QuestionAnswer,
stripeSessionStatus,
SubmitUpdateUserInfoOrNewUser,
UpdateUserInfoOrNewUser,
} from "../types/types";

const apiClient = axios.create({
Expand All @@ -10,13 +15,14 @@ const apiClient = axios.create({
});

// Get user metadata
export const getUserMetadaData = async (): Promise<AxiosResponse> => {
export const getUserMetaData = async (): Promise<AxiosResponse> => {
const response = await apiClient.get("/api/user/get-metadata", {
headers: {
"Content-Type": "application/json",
},
});

console.log("getUserMetatdat")
console.log(response)
return response;
};

Expand All @@ -28,19 +34,35 @@ export const updateUserInfo = async (data: object): Promise<AxiosResponse> => {
},
data: { data },
});

console.log("update user info")
console.log(response)
return response;
};

export const updateUserTicketInfo = async (
data: object
): Promise<AxiosResponse> => {

ticketId: number,
name: string,
email: string,
phoneNumber: string,
answers: AnswerList[]

): Promise<UpdateUserInfoOrNewUser> => {
const data = {
ticketId,
name,
email,
phoneNumber,
answers
}
const response = await apiClient.post("/api/user/user-ticket-info", data, {
headers: {
"Content-Type": "application/json",
},
});

return response;
})
console.log("update user ticket info worked")
return response.data;
};

// User membership expiry
Expand Down Expand Up @@ -85,15 +107,18 @@ export const postAttendanceUpdate = async (
// Get session status
export const getSessionStatus = async (
sessionId: string
): Promise<AxiosResponse> => {
): Promise<stripeSessionStatus> => {
const response = await apiClient.get(
`/api/stripe/session-status?session_id=${sessionId}`,
{
headers: { "Content-Type": "application/json" },
}
);

return response;
console.log("get session id")
console.log(response)

return response.data;
};

//Use this one to automatically create an Event or Membership checkout. Event checkout will decrement a ticket.
Expand All @@ -109,7 +134,7 @@ export const fetchEventOrMembershipCheckoutSecret = async (payload: {
headers: { "Content-Type": "application/json" },
}
);

console.log("FETCH EVENT OR MEBERSHcK CHECKOUT SECRETYE")
return response.data.clientSecret;
};

Expand Down
Loading

0 comments on commit a030c39

Please sign in to comment.