-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 7563b25 Author: 13bfrancis <[email protected]> Date: Thu Nov 9 12:29:20 2023 -0500 fix build error commit e3c2c18 Author: 13bfrancis <[email protected]> Date: Thu Nov 9 12:12:41 2023 -0500 fix errors with sink commit 50e4f86 Author: 13bfrancis <[email protected]> Date: Wed Nov 8 15:43:36 2023 -0500 fix index -1 error commit d489faa Author: 13bfrancis <[email protected]> Date: Wed Nov 8 15:10:45 2023 -0500 fix errors and lets reindex commit 9976017 Author: 13bfrancis <[email protected]> Date: Wed Nov 8 11:03:24 2023 -0500 fix typescript error for tombstone commit 2f6e865 Author: 13bfrancis <[email protected]> Date: Wed Nov 8 09:53:04 2023 -0500 finished up flow (fingers crossed) commit 0afb047 Author: 13bfrancis <[email protected]> Date: Wed Nov 8 09:09:52 2023 -0500 fix breadcrumb type error commit 053441f Author: 13bfrancis <[email protected]> Date: Wed Nov 8 09:06:13 2023 -0500 remove action-type from schema (not needed) commit 00eff86 Author: 13bfrancis <[email protected]> Date: Wed Nov 8 09:04:18 2023 -0500 add functionality for sinking actions to opensearch commit 43eaf99 Author: Mike Dial <[email protected]> Date: Tue Nov 7 14:47:25 2023 -0500 bringing in brian commit d8c4af4 Author: Mike Dial <[email protected]> Date: Tue Nov 7 14:27:13 2023 -0500 fix thing commit b4bbbeb Author: Mike Dial <[email protected]> Date: Tue Nov 7 14:19:37 2023 -0500 brians lib commit 8b520a5 Author: Mike Dial <[email protected]> Date: Tue Nov 7 14:15:19 2023 -0500 yut commit 1a92952 Merge: c045e36 35b4780 Author: Mike Dial <[email protected]> Date: Tue Nov 7 13:42:17 2023 -0500 Merge branch 'master' into issuerai commit c045e36 Author: Mike Dial <[email protected]> Date: Tue Nov 7 13:41:20 2023 -0500 sink commit f2d7ab6 Author: Mike Dial <[email protected]> Date: Tue Nov 7 09:38:56 2023 -0500 basic func commit e164a3e Author: Mike Dial <[email protected]> Date: Tue Nov 7 06:51:17 2023 -0500 nothing commit 9970b9c Author: Mike Dial <[email protected]> Date: Tue Nov 7 06:48:08 2023 -0500 form work commit 9943c17 Author: Mike Dial <[email protected]> Date: Tue Nov 7 06:02:30 2023 -0500 base commit 76081ac Author: Mike Dial <[email protected]> Date: Tue Nov 7 05:40:31 2023 -0500 basic routing setup commit 5917321 Author: Mike Dial <[email protected]> Date: Tue Nov 7 05:35:31 2023 -0500 Show issue rai for all records for cms user
- Loading branch information
1 parent
5549f33
commit 0b76490
Showing
16 changed files
with
549 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { z } from "zod"; | ||
|
||
export const withdrawRecordSchema = z.object({ | ||
raiWithdrawEnabled: z.boolean(), | ||
}); | ||
|
||
export type WithdrawRecord = z.infer<typeof withdrawRecordSchema>; |
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,4 +1,5 @@ | ||
export enum Action { | ||
ENABLE_RAI_WITHDRAW = "enable-rai-withdraw", | ||
DISABLE_RAI_WITHDRAW = "disable-rai-withdraw", | ||
ISSUE_RAI = "issue-rai", | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { response } from "../libs/handler"; | ||
import { APIGatewayEvent } from "aws-lambda"; | ||
import { getPackage } from "../libs/package/getPackage"; | ||
import { | ||
getAuthDetails, | ||
isAuthorized, | ||
lookupUserAttributes, | ||
} from "../libs/auth/user"; | ||
import { packageActionsForResult } from "./getPackageActions"; | ||
import { Action } from "shared-types"; | ||
import { issueRai, toggleRaiResponseWithdraw } from "./packageActions"; | ||
|
||
export const handler = async (event: APIGatewayEvent) => { | ||
try { | ||
const actionType = event.pathParameters.actionType as Action; | ||
const body = JSON.parse(event.body); | ||
console.log(actionType); | ||
console.log(body); | ||
|
||
// Check auth | ||
const result = await getPackage(body.id); | ||
const passedStateAuth = await isAuthorized(event, result._source.state); | ||
if (!passedStateAuth) | ||
return response({ | ||
statusCode: 401, | ||
body: { message: "Not authorized to view resources from this state" }, | ||
}); | ||
if (!result.found) | ||
return response({ | ||
statusCode: 404, | ||
body: { message: "No record found for the given id" }, | ||
}); | ||
const authDetails = getAuthDetails(event); | ||
const userAttr = await lookupUserAttributes( | ||
authDetails.userId, | ||
authDetails.poolId | ||
); | ||
|
||
// Check that the package action is available | ||
const actions: Action[] = packageActionsForResult(userAttr, result); | ||
if (!actions.includes(actionType)) { | ||
return response({ | ||
statusCode: 401, | ||
body: { | ||
message: `You are not authorized to perform ${actionType} on ${body.id}`, | ||
}, | ||
}); | ||
} | ||
|
||
// Call package action | ||
switch (actionType) { | ||
case Action.ISSUE_RAI: | ||
await issueRai(body.id, Date.now()); | ||
break; | ||
case Action.ENABLE_RAI_WITHDRAW: | ||
await toggleRaiResponseWithdraw(body, true); | ||
break; | ||
case Action.DISABLE_RAI_WITHDRAW: | ||
await toggleRaiResponseWithdraw(body, false); | ||
break; | ||
default: | ||
throw `No ${actionType} action available`; | ||
} | ||
return response({ | ||
statusCode: 200, | ||
body: { message: "success" }, | ||
}); | ||
} catch (error) { | ||
console.error({ error }); | ||
return response({ | ||
statusCode: 500, | ||
body: { message: "Internal server error" }, | ||
}); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as sql from "mssql"; | ||
|
||
const user = process.env.dbUser; | ||
const password = process.env.dbPassword; | ||
const server = process.env.dbIp; | ||
const port = parseInt(process.env.dbPort); | ||
const config = { | ||
user: user, | ||
password: password, | ||
server: server, | ||
port: port, | ||
database: "SEA", | ||
}; | ||
|
||
import { Action, OneMacSink, transformOnemac } from "shared-types"; | ||
Check warning on line 15 in src/services/api/handlers/packageActions.ts GitHub Actions / lint
|
||
import { produceMessage } from "../libs/kafka"; | ||
import { response } from "../libs/handler"; | ||
|
||
const TOPIC_NAME = process.env.topicName; | ||
|
||
export async function issueRai(id: string, timestamp: number) { | ||
console.log("CMS issuing a new RAI"); | ||
const pool = await sql.connect(config); | ||
const query = ` | ||
Insert into SEA.dbo.RAI (ID_Number, RAI_Requested_Date) | ||
values ('${id}' | ||
,dateadd(s, convert(int, left(${timestamp}, 10)), cast('19700101' as datetime))) | ||
`; | ||
// Prepare the request | ||
const request = pool.request(); | ||
request.input("ID_Number", sql.VarChar, id); | ||
request.input("RAI_Requested_Date", sql.DateTime, new Date(timestamp)); | ||
|
||
const result = await sql.query(query); | ||
console.log(result); | ||
await pool.close(); | ||
} | ||
|
||
export async function withdrawRai(id, timestamp) { | ||
console.log("CMS withdrawing an RAI"); | ||
} | ||
|
||
export async function respondToRai(id, timestamp) { | ||
console.log("State respnding to RAI"); | ||
} | ||
|
||
export async function withdrawPackage(id, timestamp) { | ||
console.log("State withdrawing a package."); | ||
} | ||
|
||
export async function toggleRaiResponseWithdraw( | ||
body: { id: string }, | ||
toggle: boolean | ||
) { | ||
const { id } = body; | ||
try { | ||
await produceMessage( | ||
TOPIC_NAME, | ||
id, | ||
JSON.stringify({ | ||
raiWithdrawEnabled: toggle, | ||
actionType: toggle | ||
? Action.ENABLE_RAI_WITHDRAW | ||
: Action.DISABLE_RAI_WITHDRAW, | ||
}) | ||
); | ||
|
||
return response({ | ||
statusCode: 200, | ||
body: { | ||
message: "record successfully submitted", | ||
}, | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
|
||
return response({ | ||
statusCode: 500, | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Kafka, Message } from "kafkajs"; | ||
|
||
const kafka = new Kafka({ | ||
clientId: "submit", | ||
brokers: process.env.brokerString.split(","), | ||
retry: { | ||
initialRetryTime: 300, | ||
retries: 8, | ||
}, | ||
ssl: { | ||
rejectUnauthorized: false, | ||
}, | ||
}); | ||
|
||
export const producer = kafka.producer(); | ||
|
||
export async function produceMessage( | ||
topic: string, | ||
key: string, | ||
value: string | ||
) { | ||
await producer.connect(); | ||
|
||
const message: Message = { | ||
key: key, | ||
value: value, | ||
partition: 0, | ||
headers: { source: "micro" }, | ||
}; | ||
console.log(message); | ||
|
||
try { | ||
await producer.send({ | ||
topic, | ||
messages: [message], | ||
}); | ||
console.log("Message sent successfully"); | ||
} catch (error) { | ||
console.error("Error sending message:", error); | ||
} finally { | ||
await producer.disconnect(); | ||
} | ||
} |
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.