From dbc5e7de6f1ebed032efb863e044373f99a58ad1 Mon Sep 17 00:00:00 2001
From: henryanandsr <13521004@std.stei.itb.ac.id>
Date: Wed, 15 Nov 2023 23:27:11 +0700
Subject: [PATCH] feat : add student controller to fetch into SOAP

---
 controllers/student.controller.ts | 45 ++++++++++++++++++++++++++++++-
 routes/student.routes.ts          |  3 ++-
 templates/student.ts              | 22 +++++++++++++++
 3 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 templates/student.ts

diff --git a/controllers/student.controller.ts b/controllers/student.controller.ts
index 6fcaada..f955f33 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 e45109c..9a663f1 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 0000000..6c66b6e
--- /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
-- 
GitLab