Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into dev
  • Loading branch information
paulhollmann committed Mar 10, 2024
2 parents 38d4087 + 3863638 commit 60068be
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 27 deletions.
30 changes: 22 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
APP_DEBUG=true
APP_LOG_SQL=true
APP_PORT=8001
APP_HOST=127.0.0.1
APP_KEY=<app_key>
APP_HOST=localhost
FRONTEND_URI=http://localhost:8000
APP_CORS_ALLOW=http://localhost:8000
APP_KEY=

#################
# MISC SETTINGS #
Expand All @@ -18,24 +20,36 @@ FILE_TMP_LOCATION=./storage/tmp/
# API ENDPOINT BASE URIs #
##########################
VATSIM_API_BASE=https://api.vatsim.net/api/v2
VATEUD_API_BASE=https://core.vateud.net/api
VATGER_API_BASE=https://vatsim-germany.org/api
MOODLE_API_BASE=https://api.moodle.vatsim-germany.org

VATEUD_CORE_API_KEY=

#########################
# VATSIM CONNECT CONFIG #
#########################
CONNECT_BASE=https://auth-dev.vatsim.net
CONNECT_REDIRECT_URI=<redirect_uri>
CONNECT_REDIRECT_URI=http://localhost:8000/login/callback
CONNECT_SCOPE="full_name vatsim_details email country"
CONNECT_CLIENT_ID=<connect_client_id>
CONNECT_SECRET=<connect_secret>
CONNECT_CLIENT_ID=
CONNECT_SECRET=

########################
# DATABASE INFORMATION #
########################
DB_DIALECT=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE_NAME=<db_tablename>
DB_USERNAME=<db_username>
DB_PASSWORD=<db_pw>
DB_DATABASE_NAME=
DB_USERNAME=
DB_PASSWORD=

####################
# MAIL INFORMATION #
####################
SMTP_HOST=
SMTP_PORT=
SMTP_USERNAME=
SMTP_PASSWORD=
DEBUG_EMAIL=
4 changes: 4 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export const Config = {
MOODLE_API_BASE: process.env.MOODLE_API_BASE,
},

VATEUD_CORE_CONFIG: {
API_KEY: process.env.VATEUD_CORE_API_KEY
},

CONNECT_CONFIG: {
BASE_URL: process.env.CONNECT_BASE,
CLIENT_ID: process.env.CONNECT_CLIENT_ID,
Expand Down
81 changes: 70 additions & 11 deletions src/libraries/vateud/VateudCoreLibrary.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import axios, {Method} from "axios";
import JobLibrary, {JobTypeEnum} from "../JobLibrary";
import {VateudCoreSoloCreateResponse, VateudCoreSoloCreateT, VateudCoreTypeEnum} from "./VateudCoreLibraryTypes";
import {
VateudCoreSoloCreateResponseT,
VateudCoreSoloCreateT,
VateudCoreSoloRemoveResponseT,
VateudCoreSoloRemoveT,
VateudCoreTypeEnum
} from "./VateudCoreLibraryTypes";
import {Config} from "../../core/Config";
import {UserSolo} from "../../models/UserSolo";
import Logger, {LogLevels} from "../../utility/Logger";

type SendT = {
method: Method;
endpoint: string;
data: any;
data?: any;
}

/**
* Sends the actual request to VATEUD Core
* @param props
* @param type
*/
async function _send<T>(props: SendT, type: VateudCoreTypeEnum): Promise<T | undefined> {
async function _send<T>(props: SendT): Promise<T | undefined> {
if (Config.VATEUD_CORE_CONFIG.API_KEY == null) {
Logger.log(LogLevels.LOG_ERROR, `VATEUD Core API Key missing, but attempted to update: ${props.endpoint}. Aborting.`);
return undefined;
}

try {
const res = await axios({
headers: {
'x-api-key': Config.VATEUD_CORE_CONFIG.API_KEY,
},
url: `${Config.URI_CONFIG.VATEUD_API_BASE}/${props.endpoint}`,
method: props.method,
data: props.data
Expand All @@ -28,12 +43,19 @@ async function _send<T>(props: SendT, type: VateudCoreTypeEnum): Promise<T | und
}
}

/**
* Creates a solo.
* On success, it updates the corresponding UserSolo with the returned VATEUD Solo ID
* On failure, it schedules a job which repeats the same request n times until it succeeds
* - If it fails more than n times, then it really isn't our problem anymore tbh...
* @param soloInfo
*/
async function createSolo(soloInfo: VateudCoreSoloCreateT) {
const res = await _send<VateudCoreSoloCreateResponse>({
const res = await _send<VateudCoreSoloCreateResponseT>({
endpoint: "/solo",
method: "post",
data: soloInfo
}, VateudCoreTypeEnum.SOLO_CREATE);
data: soloInfo.post_data
});
if (!res) {
// If the request fails, we schedule a job to attempt it additional times.
await JobLibrary.scheduleJob(JobTypeEnum.VATEUD_CORE, {
Expand All @@ -42,15 +64,52 @@ async function createSolo(soloInfo: VateudCoreSoloCreateT) {
data: {
solo_create: {
local_solo_id: soloInfo.local_solo_id,
user_id: soloInfo.user_id,
position: soloInfo.position,
instructor_cid: soloInfo.instructor_cid,
expire_at: soloInfo.expires_at
user_id: soloInfo.post_data.user_id,
position: soloInfo.post_data.position,
instructor_cid: soloInfo.post_data.instructor_cid,
expire_at: soloInfo.post_data.expires_at
}
}
});
return undefined;
}

await UserSolo.update({
vateud_solo_id: res.data.id
}, {
where: {
id: soloInfo.local_solo_id
}
});

return res;
}

/**
* Removes a solo.
* On failure, it schedules a job which repeats the same request n times until it succeeds
* @param soloInfo
*/
async function removeSolo(soloInfo: VateudCoreSoloRemoveT) {
const res = await _send<VateudCoreSoloRemoveResponseT>({
endpoint: `/solo/${soloInfo.vateud_solo_id}`,
method: "delete"
});
if (!res) {
await JobLibrary.scheduleJob(JobTypeEnum.VATEUD_CORE, {
type: VateudCoreTypeEnum.SOLO_REMOVE,
method: "delete",
data: {
solo_remove: {
local_solo_id: soloInfo.local_solo_id,
vateud_solo_id: soloInfo.vateud_solo_id
}
}
});
}
}

export default {
createSolo,
removeSolo
}
28 changes: 20 additions & 8 deletions src/libraries/vateud/VateudCoreLibraryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ export type VateudCorePayload = {

export type VateudCoreSoloCreateT = {
local_solo_id: number;
user_id: number;
position: string;
instructor_cid: number;
starts_at: string;
expires_at: string;
post_data: {
user_id: number;
position: string;
instructor_cid: number;
starts_at: string;
expires_at: string;
}
}

export type VateudCoreSoloCreateResponse = {
export type VateudCoreSoloCreateResponseT = {
success: boolean;
data: Array<{
data: {
id: number;
user_cid: number;
instructor_cid: number;
Expand All @@ -33,5 +35,15 @@ export type VateudCoreSoloCreateResponse = {
facility: number;
created_at: string;
updated_at: string;
}>
}
}

export type VateudCoreSoloRemoveT = {
local_solo_id: number;
vateud_solo_id: number;
}

export type VateudCoreSoloRemoveResponseT = {
success: boolean;
message: string;
}

0 comments on commit 60068be

Please sign in to comment.