Last active
January 29, 2025 15:42
-
-
Save vman/a3e2483697360b1247dab9ce33e43e98 to your computer and use it in GitHub Desktop.
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
import { | |
app, | |
HttpRequest, | |
HttpResponseInit, | |
InvocationContext, | |
} from "@azure/functions"; | |
import repairRecords from "../repairsData.json"; | |
/** | |
* This function handles the HTTP request and returns the repair information. | |
* | |
* @param {HttpRequest} req - The HTTP request. | |
* @param {InvocationContext} context - The Azure Functions context object. | |
* @returns {Promise<Response>} - A promise that resolves with the HTTP response containing the repair information. | |
*/ | |
export async function repairs( | |
req: HttpRequest, | |
context: InvocationContext | |
): Promise<HttpResponseInit> { | |
context.log("HTTP trigger function processed a request."); | |
// Initialize response. | |
const res: HttpResponseInit = { | |
status: 200, | |
jsonBody: { | |
results: repairRecords, | |
}, | |
}; | |
// Get the assignedTo query parameter. | |
const assignedTo = req.query.get("assignedTo"); | |
// If the assignedTo query parameter is not provided, return the response. | |
if (!assignedTo) { | |
return res; | |
} | |
// Filter the repair information by the assignedTo query parameter. | |
const repairs = repairRecords.filter((item) => { | |
const fullName = item.assignedTo.toLowerCase(); | |
const query = assignedTo.trim().toLowerCase(); | |
const [firstName, lastName] = fullName.split(" "); | |
return fullName === query || firstName === query || lastName === query; | |
}); | |
// Return filtered repair records, or an empty array if no records were found. | |
res.jsonBody.results = repairs ?? []; | |
return res; | |
} | |
app.http("repairs", { | |
methods: ["GET"], | |
authLevel: "anonymous", | |
handler: repairs, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment