-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refractor Planetscale to Neon (#234)
* refractor Planetscale to Neon * Seed working, next is router TRPC. * fix runtime error with cannot find DB * remove references to mysql * create a new customer when the onboarding is complete * change onDuplicateKeyUpdate to onConflictDoUpdate * change clerkUserId to userId * remove warnings for key not provided * fix type changes and allow assets and accounts to be added * fix real estate add * fixed build issue --------- Co-authored-by: Shouryan Nikam <dev.shouryan@gmail.com>
- Loading branch information
1 parent
3613edb
commit d41a151
Showing
32 changed files
with
1,108 additions
and
978 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
apps/www/app/(docs)/docs/installation/planet-scale/page.mdx
This file was deleted.
Oops, something went wrong.
185 changes: 95 additions & 90 deletions
185
apps/www/app/api/cron/update-bank-account-data/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,109 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
// import { NextRequest, NextResponse } from "next/server"; | ||
|
||
import { db, schema, sql } from "@projectx/db"; | ||
// import { db, schema, sql } from "@projectx/db"; | ||
|
||
import { env } from "@/env"; | ||
import { BankingData, connectorFacade, toConnectorEnv } from "@/lib/connector"; | ||
// import { env } from "@/env"; | ||
// import { BankingData, connectorFacade, toConnectorEnv } from "@/lib/connector"; | ||
|
||
export async function POST(req: NextRequest) { | ||
// get the bearer token from the header | ||
const authToken = (req.headers.get("authorization") || "") | ||
.split("Bearer ") | ||
.at(1); | ||
// export async function POST(req: NextRequest) { | ||
// // get the bearer token from the header | ||
// const authToken = (req.headers.get("authorization") || "") | ||
// .split("Bearer ") | ||
// .at(1); | ||
|
||
// if not found OR the bearer token does NOT equal the CRON_SECRET | ||
// TODO: Later we'll add the 2nd part of condition authToken !== env.CRON_SECRET | ||
if (!authToken) { | ||
return NextResponse.json( | ||
{ error: "Unauthorized" }, | ||
{ | ||
status: 401, | ||
}, | ||
); | ||
} | ||
// // if not found OR the bearer token does NOT equal the CRON_SECRET | ||
// // TODO: Later we'll add the 2nd part of condition authToken !== env.CRON_SECRET | ||
// if (!authToken) { | ||
// return NextResponse.json( | ||
// { error: "Unauthorized" }, | ||
// { | ||
// status: 401, | ||
// }, | ||
// ); | ||
// } | ||
|
||
try { | ||
console.log("○ [openbanking]: Handling cron bank-account-data"); | ||
// try { | ||
// console.log("○ [openbanking]: Handling cron bank-account-data"); | ||
|
||
await handleEvent(); | ||
// await handleEvent(); | ||
|
||
console.log("✓ [openbanking]: Handled cron bank-account-data"); | ||
return NextResponse.json({ received: true }, { status: 200 }); | ||
} catch (error) { | ||
const message = error instanceof Error ? error.message : "Unknown error"; | ||
console.log(`❌ [openbanking] Error when handling cron: ${message}`); | ||
return NextResponse.json({ error: message }, { status: 400 }); | ||
} | ||
} | ||
// console.log("✓ [openbanking]: Handled cron bank-account-data"); | ||
// return NextResponse.json({ received: true }, { status: 200 }); | ||
// } catch (error) { | ||
// const message = error instanceof Error ? error.message : "Unknown error"; | ||
// console.log(`❌ [openbanking] Error when handling cron: ${message}`); | ||
// return NextResponse.json({ error: message }, { status: 400 }); | ||
// } | ||
// } | ||
|
||
const handleEvent = async () => { | ||
const facade = await connectorFacade(toConnectorEnv(env.NODE_ENV)); | ||
const resourceList = await facade.listResourcesFromDB(); | ||
console.debug(`○ [openbanking] ${resourceList.length} resources`); | ||
// const handleEvent = async () => { | ||
// const facade = await connectorFacade(toConnectorEnv(env.NODE_ENV)); | ||
// const resourceList = await facade.listResourcesFromDB(); | ||
// console.debug(`○ [openbanking] ${resourceList.length} resources`); | ||
|
||
const promises = resourceList.map(({ integration, ...resource }) => { | ||
return facade.listBankingAccountData(resource, integration.connectorId); | ||
}); | ||
// const promises = resourceList.map(({ integration, ...resource }) => { | ||
// return facade.listBankingAccountData(resource, integration.connectorId); | ||
// }); | ||
|
||
const results = await Promise.allSettled(promises); | ||
// TODO: log errors | ||
const errors = results.filter((r) => r.status === "rejected"); | ||
const successes = results.filter( | ||
(r): r is PromiseFulfilledResult<BankingData[]> => r.status === "fulfilled", | ||
); | ||
// const results = await Promise.allSettled(promises); | ||
// // TODO: log errors | ||
// const errors = results.filter((r) => r.status === "rejected"); | ||
// const successes = results.filter( | ||
// (r): r is PromiseFulfilledResult<BankingData[]> => r.status === "fulfilled", | ||
// ); | ||
|
||
const dbTransactionPromiseList = successes | ||
.map((success) => success.value.map(upsertBankAccountData)) | ||
.flat(); | ||
// const dbTransactionPromiseList = successes | ||
// .map((success) => success.value.map(upsertBankAccountData)) | ||
// .flat(); | ||
|
||
// TODO: use Promise.allSettled to better log | ||
await Promise.all(dbTransactionPromiseList); | ||
}; | ||
// // TODO: use Promise.allSettled to better log | ||
// await Promise.all(dbTransactionPromiseList); | ||
// }; | ||
|
||
const upsertBankAccountData = async (data: BankingData) => { | ||
await db.transaction(async (tx) => { | ||
const accountQuery = await tx | ||
.insert(schema.account) | ||
.values(data.account) | ||
.onDuplicateKeyUpdate({ set: { name: sql`name` } }); | ||
// const upsertBankAccountData = async (data: BankingData) => { | ||
// await db.transaction(async (tx) => { | ||
// const accountQuery = await tx | ||
// .insert(schema.account) | ||
// .values(data.account) | ||
// .onConflictDoUpdate({ | ||
// target: schema.account.id, | ||
// set: { name: sql`name` } | ||
// }); | ||
|
||
await tx | ||
.insert(schema.balance) | ||
.values( | ||
data.balances.map((balance) => { | ||
return { | ||
...balance, | ||
accountId: BigInt(accountQuery.insertId), | ||
}; | ||
}), | ||
) | ||
.onDuplicateKeyUpdate({ | ||
set: { | ||
amount: sql`amount`, | ||
date: sql`date`, | ||
}, | ||
}); | ||
await tx | ||
.insert(schema.transaction) | ||
.values( | ||
data.transactions.map((transaction) => { | ||
return { | ||
...transaction, | ||
accountId: BigInt(accountQuery.insertId), | ||
}; | ||
}), | ||
) | ||
.onDuplicateKeyUpdate({ | ||
set: { | ||
amount: sql`amount`, | ||
date: sql`date`, | ||
description: sql`description`, | ||
}, | ||
}); | ||
}); | ||
}; | ||
// await tx | ||
// .insert(schema.balance) | ||
// .values( | ||
// data.balances.map((balance) => { | ||
// return { | ||
// ...balance, | ||
// accountId: BigInt(accountQuery.insertId), | ||
// }; | ||
// } | ||
// ) | ||
// .onConflictDoUpdate({ | ||
// target: schema.balance.id, | ||
// set: { | ||
// amount: sql`amount`, | ||
// date: sql`date`, | ||
// }, | ||
// }); | ||
// await tx | ||
// .insert(schema.transaction) | ||
// .values( | ||
// data.transactions.map((transaction) => { | ||
// return { | ||
// ...transaction, | ||
// accountId: BigInt(accountQuery.insertId), | ||
// }; | ||
// }), | ||
// ) | ||
// .onConflictDoUpdate({ | ||
// target: schema.transaction.id, | ||
// set: { | ||
// amount: sql`amount`, | ||
// date: sql`date`, | ||
// description: sql`description`, | ||
// }, | ||
// }); | ||
// }); | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.