{
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "const expired = (t) => {",
          "  if (!t) return true;",
          "  try {",
          "    const p = JSON.parse(atob(t.split('.')[1].replace(/-/g,'+').replace(/_/g,'/')));",
          "    return !p.exp || (p.exp * 1000) < (Date.now() + 60000);   // refresh a minute early",
          "  } catch (e) { return true; }",
          "};",
          "",
          "const logins = [",
          "  { v: 'token',      user: pm.collectionVariables.get('userName') },",
          "  { v: 'staffToken', user: pm.collectionVariables.get('staffUserName') },",
          "  { v: 'regToken',   user: pm.collectionVariables.get('regUserName') },",
          "];",
          "",
          "const need = logins.filter(l => l.user && expired(pm.collectionVariables.get(l.v)));",
          "if (need.length) {",
          "  need.forEach(l => {",
          "    pm.sendRequest({",
          "      url: pm.collectionVariables.get('baseUrl') + '/auth/login',",
          "      method: 'POST',",
          "      header: { 'Content-Type': 'application/json' },",
          "      body: { mode: 'raw', raw: JSON.stringify({ UserName: l.user, Password: pm.collectionVariables.get('password') || 'demo' }) },",
          "    }, (err, res) => {",
          "      if (err) { console.log('auto-login failed for ' + l.user, err); return; }",
          "      const j = res.json();",
          "      if (j.result !== 'Ok') { console.log('auto-login rejected for ' + l.user, j.details); return; }",
          "      const u = j.UserMainData || {};",
          "      pm.collectionVariables.set(l.v, (j.token && j.token.data) || '');",
          "      if (l.v === 'token') {",
          "        pm.collectionVariables.set('edStudId', u.ED_STUD_ID || '');",
          "        pm.collectionVariables.set('seUserId', u.SE_USER_ID || '');",
          "        pm.collectionVariables.set('seUserAccntId', u.SE_USER_ACCNT_ID || '');",
          "        pm.collectionVariables.set('facultyId', u.AS_FACULTY_INFO_ID || '');",
          "        pm.collectionVariables.set('acadYearId', u.ED_ACAD_YEAR_ID || '');",
          "        pm.collectionVariables.set('semesterId', u.ED_CODE_SEMESTER_ID || '');",
          "      } else if (l.v === 'regToken') {",
          "        pm.collectionVariables.set('regStudId', u.ED_STUD_ID || '');",
          "      } else {",
          "        pm.collectionVariables.set('staffId', u.SA_STAFF_MEMBER_ID || u.ED_STUD_ID || '');",
          "        pm.collectionVariables.set('staffSeUserId', u.SE_USER_ID || '');",
          "      }",
          "      console.log('auto-login ok: ' + l.user);",
          "    });",
          "  });",
          "}"
        ]
      }
    }
  ],
  "info": {
    "name": "عن الجامعة (قبل الدخول) · About the University",
    "description": "# عن الجامعة (قبل الدخول) — About the University\n\n<span class=\"new\">جديد</span> شاشة بتتفتح <b>من غير حساب</b> — عشان مراجع الـ App Store / Google Play، والطالب الجديد، وولي الأمر. بتعرض اسم الجامعة ولوجو وألوانها ونبذة، وأسامي الكليات والأقسام والبرامج.\n\n---\n\n**إزاي تشغّله:** شغّل `0) Login` الأول — هيملأ التوكن وكل الـ ids لوحده، وبعدها كل الريكوستات تشتغل على طول.\n\n**الحالة:** 3 شغال · 0 شغال بدون داتا · 0 ناقص",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "bearer",
    "bearer": [
      {
        "key": "token",
        "value": "{{token}}",
        "type": "string"
      }
    ]
  },
  "item": [
    {
      "name": "0) Login (run me first)",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "// Stores the identity + token so every other request just works.",
              "const j = pm.response.json();",
              "if (j.result === \"Ok\") {",
              "    const u = j.UserMainData;",
              "    pm.collectionVariables.set(\"token\",        j.token.data);",
              "    pm.collectionVariables.set(\"edStudId\",     u.ED_STUD_ID);",
              "    pm.collectionVariables.set(\"staffId\",      u.SA_STAFF_MEMBER_ID);",
              "    pm.collectionVariables.set(\"seUserId\",     u.SE_USER_ID);",
              "    pm.collectionVariables.set(\"seUserAccntId\",u.SE_USER_ACCNT_ID);",
              "    pm.collectionVariables.set(\"facultyId\",    u.AS_FACULTY_INFO_ID);",
              "    pm.collectionVariables.set(\"acadYearId\",   u.ED_ACAD_YEAR_ID);",
              "    pm.collectionVariables.set(\"semesterId\",   u.ED_CODE_SEMESTER_ID);",
              "    console.log(\"signed in:\", u.ED_STUD_ID, u.YEAR_DESC_EN, u.SEM_DESC_EN);",
              "}",
              "pm.test(\"login ok\", () => pm.expect(j.result).to.eql(\"Ok\"));"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "auth": {
          "type": "noauth"
        },
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"UserName\": \"{{userName}}\",\n  \"Password\": \"{{password}}\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/auth/login",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "auth",
            "login"
          ]
        },
        "description": "يخزّن التوكن وكل الـ ids في متغيرات الكوليكشن. **شغّله الأول.**"
      }
    },
    {
      "name": "0b) Login as STAFF (for the 👨‍🏫 requests)",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const j = pm.response.json();",
              "pm.test('staff login ok', () => pm.expect(j.result).to.eql('Ok'));",
              "const u = j.UserMainData || {};",
              "pm.collectionVariables.set('staffToken', (j.token && j.token.data) || '');",
              "pm.collectionVariables.set('staffId', u.SA_STAFF_MEMBER_ID || u.ED_STUD_ID || '');",
              "pm.collectionVariables.set('staffSeUserId', u.SE_USER_ID || '');",
              "pm.test('is NOT a student', () => pm.expect(u.IsStudent).to.eql(false));"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "auth": {
          "type": "noauth"
        },
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"UserName\": \"{{staffUserName}}\",\n  \"Password\": \"{{password}}\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/auth/login",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "auth",
            "login"
          ]
        },
        "description": "نفس إندبوينت الدخول بحساب دكتور. بيخزّن **staffToken** و**staffId**. الريكويستات اللي عليها 👨‍🏫 بتستخدمه."
      }
    },
    {
      "name": "🌐  GET Info/University",
      "request": {
        "method": "GET",
        "auth": {
          "type": "noauth"
        },
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/Info/University",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "Info",
            "University"
          ]
        },
        "description": "**✅ شغال بداتا حقيقية**\n⭐ <span class=\"new\">جديد</span> من غير توكن — بيانات الجامعة + 8 كليات بأقسامها وبرامجها · <span class=\"new\">جديد ٢</span> كل برنامج معاه <code>Plan</code> (الخطة الدراسية)؛ <code>?plans=false</code> من غيرها\n\n> **من غير توكن** — بيشتغل قبل الدخول."
      },
      "response": []
    },
    {
      "name": "🌐  GET Info/AcademicPlan?programId=10",
      "request": {
        "method": "GET",
        "auth": {
          "type": "noauth"
        },
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/Info/AcademicPlan?programId=10",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "Info",
            "AcademicPlan?programId=10"
          ]
        },
        "description": "**✅ شغال بداتا حقيقية**\n<span class=\"new\">جديد ٢</span> خطة برنامج واحد، من غير توكن — <a href=\"#wn2-plans\">التفاصيل</a>\n\n> **من غير توكن** — بيشتغل قبل الدخول."
      },
      "response": []
    },
    {
      "name": "🌐  GET Info/ByLawPage",
      "request": {
        "method": "GET",
        "auth": {
          "type": "noauth"
        },
        "header": [],
        "url": {
          "raw": "{{baseUrl}}/Info/ByLawPage",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "Info",
            "ByLawPage"
          ]
        },
        "description": "**✅ شغال بداتا حقيقية**\nلينك اللائحة (نص) — وبقى كمان جوّه <code>University.bylawUrl</code>\n\n> **من غير توكن** — بيشتغل قبل الدخول."
      },
      "response": []
    }
  ],
  "variable": [
    {
      "key": "baseUrl",
      "value": "https://api.109.123.243.32.sslip.io"
    },
    {
      "key": "userName",
      "value": "mahmoued.eprahim.23010062"
    },
    {
      "key": "staffUserName",
      "value": "atef.dahesh"
    },
    {
      "key": "regUserName",
      "value": "mahmoued.eprahim.23010062"
    },
    {
      "key": "password",
      "value": "demo"
    },
    {
      "key": "token",
      "value": ""
    },
    {
      "key": "staffToken",
      "value": ""
    },
    {
      "key": "regToken",
      "value": ""
    },
    {
      "key": "edStudId",
      "value": ""
    },
    {
      "key": "regStudId",
      "value": ""
    },
    {
      "key": "staffId",
      "value": ""
    },
    {
      "key": "staffSeUserId",
      "value": ""
    },
    {
      "key": "seUserId",
      "value": ""
    },
    {
      "key": "seUserAccntId",
      "value": ""
    },
    {
      "key": "facultyId",
      "value": ""
    },
    {
      "key": "acadYearId",
      "value": ""
    },
    {
      "key": "semesterId",
      "value": ""
    }
  ]
}