-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat(package-actions): Enable/Disable Formal RAI Response Withdraw #192
Changes from all commits
4658876
28f5988
f9d33a3
439a751
5a1b654
46d914b
d1c39ad
e22cf83
d22e541
c93ce3f
d1b4c79
34a2614
ce52488
7b06993
5549f33
0b76490
c2b9840
e57bb6c
79f6449
de0ce52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export enum Action { | ||
ENABLE_RAI_WITHDRAW = "enable-rai-withdraw", | ||
DISABLE_RAI_WITHDRAW = "disable-rai-withdraw", | ||
ISSUE_RAI = "issue-rai", | ||
} |
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" }, | ||
}); | ||
} | ||
}; |
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."); | ||
} | ||
Comment on lines
+39
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assuming these are just placeholders There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think Mike put those there |
||
|
||
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, | ||
}); | ||
} | ||
} |
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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as long as these are always in order i guess