diff --git a/controllers/student.controller.ts b/controllers/student.controller.ts index 6fcaadae6a0d705523fa1e8020022d28c87d7dc8..f955f336e0494e5c9d264761f1ba18b1ea743227 100644 --- a/controllers/student.controller.ts +++ b/controllers/student.controller.ts @@ -1,5 +1,10 @@ import { Request, Response } from "express" import axios from 'axios' +import { getStudentOfScholarship } from "../templates/student"; + +const xml2js = require("xml2js") +const soapRequest = require("easy-soap-request") +const util = require("util") export const getUserPhpInfo = async (req : Request, res : Response) => { try { @@ -24,4 +29,42 @@ export const getUserPhpInfo = async (req : Request, res : Response) => { message: "Internal server error", }); } -}; \ No newline at end of file +}; + +export const getStudentFromScholarship = async (req: Request, res: Response) => { + try { + const sid = req.params.sid + console.log("SID", sid) + + const { response } = await soapRequest({ + url: getStudentOfScholarship.url, + headers: getStudentOfScholarship.headers, + xml: util.format( + getStudentOfScholarship.body, + sid + ) + }) + + const { body } = response + console.log("body",body) + const parser = new xml2js.Parser() + const parsedBody = await parser.parseStringPromise(body) + const scholarships = + parsedBody["S:Envelope"]["S:Body"][0]["ns2:getStudentOfScholarshipResponse"][0][ + "return" + ] + res.status(200).json({ + status: "success", + message: "User retrieved successfully", + data: { + scholarships + }, + }); + } catch(error : any) { + console.error(error); + res.status(500).json({ + status: "error", + message: "Internal server error", + }); + } +} \ No newline at end of file diff --git a/routes/student.routes.ts b/routes/student.routes.ts index e45109c28099dde1918fa6e84ce853c3e678a299..9a663f106414a95765e302b86ba059160099a4cf 100644 --- a/routes/student.routes.ts +++ b/routes/student.routes.ts @@ -1,11 +1,12 @@ import express from "express" import { getUserPhpInfo, + getStudentFromScholarship } from "../controllers/student.controller" const router = express.Router() router.get("/user/:uid", getUserPhpInfo) - +router.get("/scholarship/user/:sid", getStudentFromScholarship) module.exports = router \ No newline at end of file diff --git a/templates/student.ts b/templates/student.ts new file mode 100644 index 0000000000000000000000000000000000000000..6c66b6ee68cd6ca375ca235ec3be2beb72acb8b6 --- /dev/null +++ b/templates/student.ts @@ -0,0 +1,22 @@ +const headers = { + "Content-Type": "text/xml;charset=UTF-8", + "X-API-KEY": process.env.SOAP_API_KEY +} + +const getStudentOfScholarshipURL = process.env.SOAP_URL + "/ws/ScholarshipAcceptance?wsdl" + +const getStudentOfScholarshipTemplate = ` +<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> + <Body> + <getStudentOfScholarship xmlns="http://services.soap.orden.com/"> + <scholarship_id_rest xmlns="">%d</scholarship_id_rest> + </getStudentOfScholarship> + </Body> +</Envelope> +` + +export const getStudentOfScholarship = { + url : getStudentOfScholarshipURL, + headers : headers, + body: getStudentOfScholarshipTemplate +} \ No newline at end of file