diff --git a/components/ImageUpload.vue b/components/ImageUpload.vue index 9d36e11b..3f85731e 100644 --- a/components/ImageUpload.vue +++ b/components/ImageUpload.vue @@ -25,7 +25,6 @@ const onFile = async (event: Event) => { const imageDataObj = { contentLength: file.size, contentType: file.type, - file, pageCuid: props.pageCuid } const imageData = await $fetch('/api/image_upload', { diff --git a/pages/Page/[id].vue b/pages/Page/[id].vue index 0dc5a764..ddd7fc71 100644 --- a/pages/Page/[id].vue +++ b/pages/Page/[id].vue @@ -318,7 +318,7 @@ setImageAutoSlide() .text-xs.text-gray-600.pt-5 Amount Donated: {{ donationFormat(comment.amount) }} CVReplySystem(:pageCuid="id" :familyCuid="familyCuid" :replies="replies" @displayReply="displayReply") .py-4.grid.row-span-3.gap-2(v-if="replies?.length") - .p-2.bg-white.rounded-lg.mb-2.shadow-md.pb-4(v-for="(reply,i) in replies" :key="i") + .p-2.bg-white.rounded-lg.mb-2.shadow-md.pb-4(v-for="(reply,i) in replies.filter(item => !item.suspended)" :key="i") .flex.justify-between.gap-5.pd-4 .ml-1.text-lg.font-bold {{reply.name}} .ml-1.text-lg {{dateFormat(reply.date)}} diff --git a/server/api/familiesReports.get.ts b/server/api/familiesReports.get.ts index efeb6b48..2a76f5d8 100644 --- a/server/api/familiesReports.get.ts +++ b/server/api/familiesReports.get.ts @@ -1,8 +1,5 @@ -import { PrismaClient } from "@prisma/client" import {loginRedirectUrl} from "./auth0" -import type { User } from "@/types.d.ts" -import type { Family, Page } from "@/types.d.ts" -const prisma = new PrismaClient() +import type { User, Family, Page } from "@/types.d.ts" /* * /Users @@ -16,21 +13,21 @@ export default defineEventHandler(async event => { const end_date_date = end_date as Date console.log(typeof end_date) if(event.context.user?.user_role === "advocate" || event.context.user?.user_role === "admin") { - const [ count, count_date_ranged, all_families, paginated_pages, date_ranged_pages ] = await prisma.$transaction([ - prisma.page.count(), - prisma.page.count({ where: { + const [ count, count_date_ranged, all_families, paginated_pages, date_ranged_pages ] = await event.context.client.$transaction([ + event.context.client.page.count(), + event.context.client.page.count({ where: { [date_field as string]: { gte: new Date(start_date as string), lte: new Date(end_date as string) } }}), - prisma.family.findMany({ + event.context.client.family.findMany({ include: { Pages: true, FamilyMembers: true, AdvocateResponsible: true }}), - prisma.page.findMany({ + event.context.client.page.findMany({ include: { Family: { include: { @@ -41,7 +38,7 @@ export default defineEventHandler(async event => { skip: (parseInt(dimensions as string) as number) * (page_number as number), take: (parseInt(dimensions as string) as number) }), - prisma.page.findMany({ + event.context.client.page.findMany({ include: { Family: { include: { diff --git a/server/api/family.post.ts b/server/api/family.post.ts index 1a511a94..eecf680a 100644 --- a/server/api/family.post.ts +++ b/server/api/family.post.ts @@ -1,6 +1,4 @@ -import { PrismaClient } from "@prisma/client" import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses" -const prisma = new PrismaClient() const sesClient = new SESClient({ region: "us-east-1" }); import {loginRedirectUrl} from "../api/auth0" import emailTemplates from "email-templates" @@ -44,7 +42,7 @@ const body = await readBody(event); phone, address } = body if(event.context.user?.user_role === "advocate" || event.context.user?.user_role === "admin") { try { - const queryRes = await event.context.client?.family.create({ + const queryRes = await event.context.client.family.create({ data: { family_name: family_name, AdvocateResponsible: { diff --git a/server/api/family_donations.get.ts b/server/api/family_donations.get.ts index f4d9c6e5..0768f507 100644 --- a/server/api/family_donations.get.ts +++ b/server/api/family_donations.get.ts @@ -1,5 +1,3 @@ -import { PrismaClient } from "@prisma/client" -const prisma = new PrismaClient() import {loginRedirectUrl} from "../api/auth0" /* @@ -15,7 +13,7 @@ export default defineEventHandler(async event => { } if(event.context.user?.user_role === "admin") { console.log(family_cuid) - const queryRes = await prisma.pageDonation.findMany({ + const queryRes = await event.context.client.pageDonation.findMany({ where: { familyCuid: family_cuid as string, success: true diff --git a/server/api/family_pages.get.ts b/server/api/family_pages.get.ts index 633c80f4..93e0cd1a 100644 --- a/server/api/family_pages.get.ts +++ b/server/api/family_pages.get.ts @@ -1,6 +1,4 @@ -import { PrismaClient } from "@prisma/client" import {loginRedirectUrl} from "../api/auth0" -const prisma = new PrismaClient() /* * /PageList/cuid @@ -15,11 +13,11 @@ export default defineEventHandler(async event => { } if(event.context.user?.user_role === "advocate" || event.context.user?.user_role == "admin" || event.context.user?.familyCuid == family_cuid as string) { - const [count, pagesResult, pagesUnpaginated] = await prisma.$transaction([ - prisma.page.count( { where: { + const [count, pagesResult, pagesUnpaginated] = await event.context.client.$transaction([ + event.context.client.page.count( { where: { familyCuid : family_cuid as string }}), - prisma.page.findMany({ + event.context.client.page.findMany({ where: { familyCuid : family_cuid as string }, @@ -39,7 +37,7 @@ export default defineEventHandler(async event => { } } }), - prisma.page.findMany({ + event.context.client.page.findMany({ where: { familyCuid : family_cuid as string }, diff --git a/server/api/family_transaction_payout.post.ts b/server/api/family_transaction_payout.post.ts index e8c5b8b5..d65a6817 100644 --- a/server/api/family_transaction_payout.post.ts +++ b/server/api/family_transaction_payout.post.ts @@ -1,5 +1,3 @@ -import { PrismaClient } from "@prisma/client" -const prisma = new PrismaClient() import {loginRedirectUrl} from "../api/auth0" import { nanoid } from "nanoid" import Stripe from "stripe" @@ -13,7 +11,7 @@ export default defineEventHandler(async event => { if(event.context.user?.user_role == "admin") { // update success flag in transaction - const family = await prisma.family.findFirst({ + const family = await event.context.client.family.findFirst({ where: { cuid: familyCuid } @@ -45,8 +43,8 @@ export default defineEventHandler(async event => { transfer.balance_transaction as string, ); console.log(transferBalanceTransaction.fee_details) - await prisma.$transaction([ - prisma.donationPayout.create({ + await event.context.client.$transaction([ + event.context.client.donationPayout.create({ data: { transaction_id: transfer.id, amount: body.amount, @@ -62,7 +60,7 @@ export default defineEventHandler(async event => { } } }}), - prisma.page.update({ + event.context.client.page.update({ where: { cuid: body.pageCuid as string }, diff --git a/server/api/image.get.ts b/server/api/image.get.ts index 16987200..3d9d4989 100644 --- a/server/api/image.get.ts +++ b/server/api/image.get.ts @@ -1,5 +1,3 @@ -import { PrismaClient } from "@prisma/client" -const prisma = new PrismaClient() /* * /EditPage/cuid and /Page/cuid @@ -11,7 +9,7 @@ export default defineEventHandler(async event => { const { pageCuid } = getQuery(event) // retrives all the images that belong to a family page to a family page - const queryRes = await prisma.image.findMany({ + const queryRes = await event.context.client.image.findMany({ where: { pageCuid : pageCuid as string } diff --git a/server/api/page.get.ts b/server/api/page.get.ts index babdadd1..ad17328e 100644 --- a/server/api/page.get.ts +++ b/server/api/page.get.ts @@ -1,5 +1,3 @@ -import { PrismaClient } from "@prisma/client" -const prisma = new PrismaClient() /* * /EditPage/cuid or /Page/cuid @@ -13,7 +11,7 @@ export default defineEventHandler(async event => { if( (cuid as string) == "0" || cuid == undefined){ return false } - const queryRes = await prisma.page.findFirst({ + const queryRes = await event.context.client.page.findFirst({ where: { cuid : cuid as string }, diff --git a/server/api/page.post.ts b/server/api/page.post.ts index 921e0633..362d1379 100644 --- a/server/api/page.post.ts +++ b/server/api/page.post.ts @@ -1,8 +1,5 @@ -import { PrismaClient } from "@prisma/client" -import { donationFormat } from "@/utils" import type { Image } from "@/types.d.ts" import {loginRedirectUrl} from "../api/auth0" -const prisma = new PrismaClient() /* * /EditPage/0 @@ -18,13 +15,13 @@ export default defineEventHandler(async event => { data.donation_goal = Math.trunc(data.donation_goal * 100); data.amount_raised = Math.trunc(data.amount_raised * 100); try { - const family = await prisma.family.findFirst({ + const family = await event.context.client.family.findFirst({ where: { cuid: familyCuid as string } }) data.status = family?.stripe_account_id ? 'active' : 'no family stripe account' // Creates a new entry in the database in the page model to a specfic user - const queryRes = await prisma.page.create({ + const queryRes = await event.context.client.page.create({ data: { ...data, cuid: undefined, User: { @@ -44,7 +41,7 @@ export default defineEventHandler(async event => { // Reason: the cuid for the family page is created in the above in the creation query await Promise.all( Images.map(async (image: Image) => - await prisma.image.update({ + await event.context.client.image.update({ where: { cuid: image.cuid }, diff --git a/server/api/page.put.ts b/server/api/page.put.ts index d5c3f327..7b6600a3 100644 --- a/server/api/page.put.ts +++ b/server/api/page.put.ts @@ -1,7 +1,5 @@ -import { PrismaClient } from "@prisma/client" -import {loginRedirectUrl} from "../api/auth0" +import {loginRedirectUrl} from "./auth0" import type { Image } from "@/types.d.ts" -const prisma = new PrismaClient() /* * /EditPage/cuid @@ -27,7 +25,7 @@ export default defineEventHandler(async event => { console.log("amount raised after removing formating ", data.amount_raised) } // updates a pre-existing page - const queryRes = await prisma.page.update({ + const queryRes = await event.context.client.page.update({ where: { cuid: data.cuid }, @@ -35,14 +33,17 @@ export default defineEventHandler(async event => { ...data } }); - + // Initially the images are not linked to a family page, so we add it here // Reason: the cuid for the family page is created in the above in the creation query // For page edit, images without a pageCuid are processed + // Changed the logic to work for puts a while ago. + // Image attaches to page on upload for existing page. + // Remove below? /*await Promise.all( Images.forEach(async (image: Image) => { if(image.pageCuid == null) { - await prisma.image.update({ + await event.context.prisma.image.update({ where: { cuid: image.cuid }, diff --git a/server/api/page_list.get.ts b/server/api/page_list.get.ts index 3556b9db..503ddf1c 100644 --- a/server/api/page_list.get.ts +++ b/server/api/page_list.get.ts @@ -1,6 +1,4 @@ -import { PrismaClient } from "@prisma/client" import {loginRedirectUrl} from "../api/auth0" -const prisma = new PrismaClient() /* * /PageList/cuid @@ -17,9 +15,9 @@ export default defineEventHandler(async event => { } if(event.context.user?.user_role === "admin" && cuid === "") { - const [count, pagesResult] = await prisma.$transaction([ - prisma.page.count(), - prisma.page.findMany({ + const [count, pagesResult] = await event.context.client.$transaction([ + event.context.client.page.count(), + event.context.client.page.findMany({ skip: page_number as number * 12, take: 12, include: { @@ -31,7 +29,7 @@ export default defineEventHandler(async event => { } }}) ]) - console.log(pagesResult) + //console.log(pagesResult) return { Pagination: { total: count @@ -39,8 +37,8 @@ export default defineEventHandler(async event => { data: pagesResult }; } else if(event.context.user?.user_role === "advocate" && cuid === "") { - const [count, pagesResult] = await prisma.$transaction([ - prisma.page.count({ + const [count, pagesResult] = await event.context.client.$transaction([ + event.context.client.page.count({ where: { Family: { AdvocateResponsible: { @@ -49,7 +47,7 @@ export default defineEventHandler(async event => { }, } }), - prisma.page.findMany({ + event.context.client.page.findMany({ skip: page_number as number * 12, take: 12, where: { @@ -68,7 +66,7 @@ export default defineEventHandler(async event => { } }}) ]) - console.log("here", pagesResult) + //console.log("here", pagesResult) return { Pagination: { total: count @@ -76,15 +74,15 @@ export default defineEventHandler(async event => { data: pagesResult }; } else if(event.context.user?.user_role === "admin" && cuid !=="" || event.context.user?.user_role === "advocate" && cuid !=="" || event.context.user?.cuid == cuid || event.context.user?.familyCuid == cuid){ - const [count, pagesResult, pagesUnpaginated] = await prisma.$transaction([ - prisma.page.count({ where: { + const [count, pagesResult, pagesUnpaginated] = await event.context.client.$transaction([ + event.context.client.page.count({ where: { OR: [ { userCuid: cuid as string }, { familyCuid: cuid as string } ] }}), - prisma.page.findMany({ + event.context.client.page.findMany({ where: { OR: [ { userCuid: cuid as string, @@ -104,7 +102,7 @@ export default defineEventHandler(async event => { } } }), - prisma.page.findMany({ + event.context.client.page.findMany({ where: { OR: [ { userCuid: cuid as string }, @@ -122,7 +120,7 @@ export default defineEventHandler(async event => { }) ]) - console.log(pagesResult) + //console.log(pagesResult) return { Pagination: { total: count }, diff --git a/server/api/pages.get.ts b/server/api/pages.get.ts index 6b354d3d..0e37d4d3 100644 --- a/server/api/pages.get.ts +++ b/server/api/pages.get.ts @@ -1,6 +1,4 @@ -import { PrismaClient } from "@prisma/client" import type { Page } from '@/types.d.ts' -const prisma = new PrismaClient() /* * /PageList/cuid @@ -14,9 +12,9 @@ const runtime = useRuntimeConfig() if(event.context.user?.cuid != undefined) { //if the user is not logged in, do not let them see all the pages const { searchQuery, page_number, isPageList, order, sortedColumn } = getQuery(event); if((searchQuery as string) == "" && event.context.user?.user_role == "admin" && (isPageList == 1) as boolean) { - const [count, pagesResult] = await prisma.$transaction([ - prisma.page.count(), - prisma.page.findMany({ + const [count, pagesResult] = await event.context.client.$transaction([ + event.context.client.page.count(), + event.context.client.page.findMany({ skip: page_number as number * 12, take: 12, include: { @@ -41,8 +39,8 @@ if(event.context.user?.cuid != undefined) { //if the user is not logged in, do n // Makes sure that an empty searchQuery returns no results and that searchQueries with all spaces return no results (prevents returning all pages with a first and last name using a space). if((searchQuery as string) != "" && searchQuerySpacesRemoved.length != 0) { // Pagination via taking the absolute page number with 12 records per page - const [count, pagesResult, unsortedPages] = await prisma.$transaction([ - prisma.page.count({ + const [count, pagesResult, unsortedPages] = await event.context.client.$transaction([ + event.context.client.page.count({ where: { OR: [ { page_first_name: { @@ -54,7 +52,7 @@ if(event.context.user?.cuid != undefined) { //if the user is not logged in, do n mode: 'insensitive', }}] }}), - prisma.page.findMany({ + event.context.client.page.findMany({ orderBy: { [(sortedColumn as string) || 'page_last_name'] : (order as string) || 'asc' } as const, @@ -72,7 +70,7 @@ if(event.context.user?.cuid != undefined) { //if the user is not logged in, do n skip: page_number as number * 12, take: 12, }), - prisma.page.findMany({ + event.context.client.page.findMany({ where: { OR: [ { page_first_name: { diff --git a/server/api/replies.post.ts b/server/api/replies.post.ts index 73b00c54..13154732 100644 --- a/server/api/replies.post.ts +++ b/server/api/replies.post.ts @@ -1,12 +1,8 @@ -import { PrismaClient } from "@prisma/client"; - -const prisma = new PrismaClient(); - export default defineEventHandler(async (event) => { //try { const {pageCuid, familyCuid,replyData} = await readBody(event) console.log(replyData._value) - const newReply = await prisma.reply.create({ + const newReply = await event.context.client.reply.create({ data: { ...replyData._value} }); diff --git a/server/api/replies_suspend.put.ts b/server/api/replies_suspend.put.ts index 40387b39..51331c3f 100644 --- a/server/api/replies_suspend.put.ts +++ b/server/api/replies_suspend.put.ts @@ -1,11 +1,7 @@ -import { PrismaClient } from "@prisma/client"; - -const prisma = new PrismaClient(); - export default defineEventHandler(async (event) => { const {replyData, suspended} = await readBody(event) console.log(replyData) - const newReply = await prisma.reply.update({ + const newReply = await event.context.client.reply.update({ where: { cuid: replyData.cuid } , data: { suspended: suspended } }); diff --git a/server/middleware/auth.ts b/server/middleware/auth.ts index a2cc82e2..cbc94475 100644 --- a/server/middleware/auth.ts +++ b/server/middleware/auth.ts @@ -24,7 +24,7 @@ export default defineEventHandler(async event => { fs.readFileSync(process.cwd()+"/cert-dev.pem") ) event.context.claims = claims - event.context.user = await event.context.client?.user.findFirst( + event.context.user = await event.context.client.user.findFirst( { where: { email: claims.email } , @@ -51,7 +51,6 @@ export default defineEventHandler(async event => { setCookie(event,'cvtoken','') setCookie(event,'cvuser','') return await sendRedirect(event, logoutRedirectUrl(cvtoken)) // todo: add error message after failed log in attempt - return await sendRedirect(event, loginRedirectUrl()); } // include pages ids to check if that's the family's page. setCookie(event, "cvuser", JSON.stringify(event.context.user)) @@ -145,6 +144,11 @@ export default defineEventHandler(async event => { } } catch(e){ console.error(e) + if((e.message as unknown as string).includes('jwt expired')) { + setCookie(event,'cvtoken','') + setCookie(event,'cvuser','') + return await sendRedirect(event, logoutRedirectUrl(cvtoken)) + } } } }