forked from jackcrane/slu-open-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Full TDX integration #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bellao314
wants to merge
43
commits into
main
Choose a base branch
from
tdx-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Full TDX integration #174
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
0ad54e9
added tdx plug-in folder
bellao314 d94f77c
clean up tdx add on
bellao314 ffdc6d0
tdx ui work in progress
bellao314 721a458
added input field to Jobs page (will continue to work on auth and oth…
bellao314 dd319b5
linting
bellao314 aeb0d01
linting pt 2
bellao314 ed968b3
renamed functions and changed alert to Toast
bellao314 e7715e1
fixed Toast implementation
bellao314 cab05d4
Merge pull request #120 from oss-slu/118-tdx-part-1
bellao314 1d68636
Merge branch 'main' of https://github.com/oss-slu/core_desk into 127-…
bellao314 85a075f
renamed add-ons folder
bellao314 924053f
getting somewhere
bellao314 b517564
success
bellao314 ed49454
linting
bellao314 f697b8a
clean up UI
bellao314 2980258
update dueDate to be week from import
bellao314 6e912a6
filter by ID instead of index for Attributes
bellao314 ee15f21
edited description
bellao314 cc9ea1f
linting?
bellao314 4f91ea5
lintttt
bellao314 4f1b68f
remove jobOptions array
bellao314 bb136a8
toast for error catching
bellao314 27c45dd
remove comments
bellao314 2f2e89e
more error catching
bellao314 f699b2c
loading message while fetching ticket
bellao314 fca20e7
fix fetching ticket
bellao314 6675eaf
Merge pull request #131 from oss-slu/127-tdx-pt-2
bellao314 f808e60
experimenting with auth
bellao314 284909d
added SSO (will return as unauthorized for now)
bellao314 50ae2cb
lint app
bellao314 e779afa
lint api
bellao314 4c4b398
lint api pt 2
bellao314 d96e85c
correct hardcoded redirection; added console log
bellao314 4456443
update index.js
bellao314 d97130e
use env var for base url
bellao314 b4821b8
change sessionStorage to localStorage
bellao314 3ba3208
toast.promise
bellao314 cbed631
added tdx input as modal, just need to get it to render
bellao314 ac18d67
lint?
bellao314 83ba575
TDX modal renders, and text input does not re-render with each new ch…
bellao314 65d44f4
lint app
bellao314 3f23dbe
rename token and auto reload after auth
bellao314 c4c1ed9
Merge branch 'main' into tdx-integration
jackcrane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -100,45 +100,54 @@ export const post = [ | |
| } | ||
|
|
||
| if (onBehalfOfUserEmail) { | ||
| const user = await prisma.user.create({ | ||
| data: { | ||
| email: onBehalfOfUserEmail, | ||
| firstName: onBehalfOfUserFirstName, | ||
| lastName: onBehalfOfUserLastName, | ||
| }, | ||
| }); | ||
|
|
||
| const shopsToJoin = await prisma.shop.findMany({ | ||
| let user = await prisma.user.findFirst({ | ||
| where: { | ||
| autoJoin: true, | ||
| email: onBehalfOfUserEmail, | ||
| }, | ||
| }); | ||
|
|
||
| for (const shop of shopsToJoin) { | ||
| await prisma.userShop.create({ | ||
| if (!user) { | ||
| const user = await prisma.user.create({ | ||
| data: { | ||
| userId: user.id, | ||
| shopId: shop.id, | ||
| active: true, | ||
| email: onBehalfOfUserEmail, | ||
| firstName: onBehalfOfUserFirstName, | ||
| lastName: onBehalfOfUserLastName, | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
| const shopsToJoin = await prisma.shop.findMany({ | ||
| where: { | ||
| autoJoin: true, | ||
| }, | ||
| }); | ||
|
|
||
| for (const shop of shopsToJoin) { | ||
| await prisma.userShop.create({ | ||
| data: { | ||
| userId: user.id, | ||
| shopId: shop.id, | ||
| active: true, | ||
| }, | ||
| }); | ||
|
|
||
| await prisma.logs.create({ | ||
| data: { | ||
| userId: user.id, | ||
| type: LogType.USER_CONNECTED_TO_SHOP, | ||
| shopId: shop.id, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| await prisma.logs.create({ | ||
| data: { | ||
| userId: user.id, | ||
| type: LogType.USER_CONNECTED_TO_SHOP, | ||
| shopId: shop.id, | ||
| type: LogType.USER_CREATED, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| await prisma.logs.create({ | ||
| data: { | ||
| userId: user.id, | ||
| type: LogType.USER_CREATED, | ||
| }, | ||
| }); | ||
|
|
||
| userToCreateJobAs = user.id; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -25,5 +25,6 @@ export { useBillingGroup } from "./useBillingGroup"; | |
| export { useBillingGroupInvitations } from "./useBillingGroupInvitations"; | ||
| export { useBillingGroupInvitation } from "./useBillingGroupInvitation"; | ||
| export { useBillingGroupUser } from "./useBillingGroupUser"; | ||
| export { useTDXTickets } from "./useTDXTickets"; | ||
| export { useBillingGroupLedger } from "./useBillingGroupLedger"; | ||
| export { useShopLedger } from "./useShopLedger"; | ||
This file contains hidden or 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,4 +1,5 @@ | ||
| import React, { useState, useEffect } from "react"; | ||
| import toast from "react-hot-toast"; | ||
| import { authFetch } from "#url"; | ||
| import { useModal } from "#modal"; | ||
| import { Input, Spinner, Util, Switch, Card } from "tabler-react-2"; | ||
|
|
@@ -8,6 +9,7 @@ import { useUserShop } from "./useUserShop"; | |
| import { useAuth } from "#useAuth"; | ||
| import { ShopUserPicker } from "#shopUserPicker"; | ||
| import { BillingGroupPicker } from "../components/billingGroupPicker/BillingGroupPicker"; | ||
| import { useTDXTickets } from "./useTDXTickets"; | ||
|
|
||
| const toLocalDateInputValue = (date) => { | ||
| const localDate = new Date(date); | ||
|
|
@@ -185,6 +187,69 @@ const CreateJobModalContent = ({ | |
| ); | ||
| }; | ||
|
|
||
| const CreateTDXImportModalContent = ({ | ||
| onSubmit | ||
| }) => { | ||
| const {shopId} = useParams(''); | ||
| const [auth] = useState({ tdxToken: localStorage.getItem('tdx_bearer_token') }); | ||
| const [ticketId, setTicketId] = useState(''); | ||
| const { loading, handleLogin, fetchTicket } = useTDXTickets(); | ||
| if (!auth.tdxToken) { | ||
| return ( | ||
| <div> | ||
| <div> | ||
| <Button | ||
| loading={loading} | ||
| onClick={async () => { | ||
| handleLogin(shopId); | ||
| }} | ||
| > | ||
| Log Into TDNext | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
| return ( | ||
| <div className="gap-2 border-b-5 border-b-black"> | ||
| <Input | ||
| value={ticketId} | ||
| onChange={(e) => setTicketId(e)} | ||
| label="TDNext Ticket ID" | ||
| placeholder="Ticket ID" | ||
| /> | ||
| <Button | ||
| loading={loading} | ||
| onClick={async () => { | ||
| const data = await fetchTicket(ticketId); | ||
| toast.promise(data, { | ||
| loading: "Fetching ticket from TDX...", | ||
| success: "Successfully imported ticket.", | ||
| error: "Error when fetching", | ||
| }); | ||
| if (data && onSubmit) { | ||
| // Call onSubmit with positional arguments to match _createJob | ||
| onSubmit( | ||
| data.title, | ||
| data.description, | ||
| data.dueDate, | ||
| data.onBehalfOf, | ||
| data.onBehalfOfUserId, | ||
| data.onBehalfOfUserEmail, | ||
| data.onBehalfOfUserFirstName, | ||
| data.onBehalfOfUserLastName, | ||
| data.onBehalfOfBillingGroup, | ||
| data.onBehalfOfBillingGroupId | ||
| ); | ||
| } | ||
| }} | ||
| > | ||
| Import Ticket | ||
| </Button> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const useJobs = (shopId) => { | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(null); | ||
|
|
@@ -235,6 +300,11 @@ export const useJobs = (shopId) => { | |
| text: <CreateJobModalContent onSubmit={_createJob} />, | ||
| }); | ||
|
|
||
| const { modal: tdxModal, ModalElement: TDXModalElement } = useModal({ | ||
| title: "Import from TDNext", | ||
| text: <CreateTDXImportModalContent onSubmit={_createJob}/> | ||
| }); | ||
|
|
||
| const CreateSimpleSubPage = () => { | ||
| const [title, setTitle] = useState(""); | ||
| const [description, setDescription] = useState(""); | ||
|
|
@@ -337,6 +407,15 @@ export const useJobs = (shopId) => { | |
| }); | ||
| }; | ||
|
|
||
| const importTDXJob = async () => { | ||
| tdxModal({ | ||
| title: "Import ticket from TDNext", | ||
| text: ( | ||
| <CreateTDXImportModalContent onSubmit={_createJob} /> | ||
| ) | ||
| }) | ||
| }; | ||
|
|
||
| const updateJob = async (jobId, newJob) => { | ||
| const job = jobs.find((j) => j.id === jobId); | ||
| if (job.finalized) { | ||
|
|
@@ -383,7 +462,9 @@ export const useJobs = (shopId) => { | |
| refetch: fetchJobs, | ||
| CreateSimpleSubPage, | ||
| ModalElement, | ||
| TDXModalElement, | ||
| createJob, | ||
| importTDXJob, | ||
| opLoading, | ||
| updateJob, | ||
| }; | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { useState } from 'react'; | ||
| import { authFetch } from "#url"; | ||
| import toast from 'react-hot-toast'; | ||
|
|
||
| export const useTDXTickets = () => { | ||
| const [auth, setAuth] = useState({ tdxToken: localStorage.getItem('tdx_bearer_token') }); | ||
| const [ticketId, setTicketId] = useState(''); | ||
| const [ticket, setTicket] = useState(null); | ||
| const [loading, setLoading] = useState(false); | ||
| const [error, setError] = useState(null); | ||
|
|
||
| const currentDate = new Date(); | ||
| const nextWeekDate = new Date(currentDate); | ||
|
|
||
| const handleLogin = (shopId) => { | ||
| setLoading(true); | ||
|
|
||
| const relayUrl = `${import.meta.env.VITE_API_BASE_URL}/api/tdx-auth?shopId=${shopId}`; // update later | ||
| const loginWindow = window.open(relayUrl, "OktaLogin", "width=500,height=600"); | ||
|
|
||
| const timer = setInterval(() => { | ||
| try { | ||
| if (loginWindow.location.href.includes(window.location.hostname)) { | ||
| const urlParams = new URLSearchParams(loginWindow.location.search); | ||
| const token = urlParams.get('token'); | ||
|
|
||
| if (token) { | ||
| localStorage.setItem('tdx_bearer_token', token); | ||
| setAuth({ tdxToken: token }); | ||
|
|
||
| loginWindow.close(); | ||
| clearInterval(timer); | ||
| setLoading(false); | ||
| toast.success("TDX Authenticated"); | ||
| window.location.reload(); | ||
| } | ||
| } | ||
| } catch (e) { | ||
| toast.error(e); | ||
| setError(e); | ||
| } | ||
| }, 1000); | ||
| }; | ||
|
|
||
| const fetchTicket = async (id) => { | ||
| var ticket; | ||
|
|
||
| if (!id) { | ||
| toast.error("Please enter a Ticket ID"); | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| // Pointing to your proxy endpoint | ||
| try { | ||
| const r = await authFetch(`/api/tdx-ticket?ticketId=${id}&token=${auth.tdxToken}`); | ||
| ticket = await r.json(); | ||
| setTicket(ticket); | ||
| } catch (error) { | ||
| setError(error); | ||
| toast.error(`Ticket fetch failed: ${error}`); | ||
| } | ||
|
|
||
| // Extract values from TDX Attributes | ||
| // const costAttr = ticket?.Attributes.find(item => item.ID === 2312)?.Value || 0.00; | ||
| const descriptionAttr = `${ticket?.Attributes.find(item => item.ID === 2328)?.Value}; ${ticket?.Attributes.find(item => item.ID === 2311)?.Value}`; | ||
|
|
||
| // Map the TDX response to the specific order _createJob expects | ||
| return { | ||
| title: ticket?.Title ? `TDX: ${ticket?.Title}` : "No Title Available", | ||
| description: descriptionAttr, | ||
| dueDate: nextWeekDate.setDate(currentDate.getDate() + 7), // Defaulting to week from current day | ||
| onBehalfOf: true, | ||
| onBehalfOfUserId: ticket?.RequestorUID || '', | ||
| onBehalfOfUserEmail: ticket?.RequestorEmail || '', | ||
| onBehalfOfUserFirstName: ticket?.RequestorFullName?.split(" ")[0] || "Unknown", | ||
| onBehalfOfUserLastName: ticket?.RequestorFullName?.split(" ")[1] || "Unknown", | ||
| onBehalfOfBillingGroup: false, // Defaulting as per your previous state | ||
| onBehalfOfBillingGroupId: null | ||
| }; | ||
| } catch (error) { | ||
| setError(error.message); | ||
| toast.error(error.message); | ||
| return null; | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| ticket, | ||
| ticketId, | ||
| setTicketId, | ||
| loading, | ||
| handleLogin, | ||
| fetchTicket, | ||
| error | ||
| }; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Information exposure through a stack trace Medium
Copilot Autofix
AI 3 months ago
In general, to fix this problem you should avoid sending raw error objects or stack traces to the client. Instead, log the detailed error information on the server (e.g., using
console.erroror a logging library), and send a generic, user-friendly error message with an appropriate HTTP status code.For this specific code, the best fix that preserves existing functionality is to update the
/api/tdx-authroute’scatchblock (lines 405–407). Instead ofres.status(500).send(error);, log the error server-side (if not already logged) and send a generic error message such as"Internal Server Error"or a more domain-specific but non-revealing message. The rest of the behavior (status code 500, route logic) remains unchanged. Concretely:api/index.js, within the/api/tdx-authroute, modify thecatch (error)block to:console.error("TDX Auth Error:", error);(or similar) to keep detailed diagnostics on the server.res.status(500).send("Internal Server Error");(or equivalent) instead of sendingerrordirectly.No new imports or external dependencies are needed;
consoleis globally available in Node.