Skip to content

Commit

Permalink
added orbi integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Muncherkin committed Apr 21, 2024
1 parent a100d60 commit 009b0bb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ WIKI_PASSWORD=
WIKI_BASE_URL=https://wiki.esek.se

SKIP_ACCESS_CHECKS=false
POST_ACCESS_COOLDOWN_DAYS=90
POST_ACCESS_COOLDOWN_DAYS=90

# Orbi service
ORBI=http://localhost:6970
ORBI_API_TOKEN=
11 changes: 11 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ const JWT = {
SECRET: (process.env.JWT_SECRET as string) ?? '',
};

/**
* Config for Orbi - our orbi API service
* @param {string} URL - The base URL for the Orbi API
* @param {string} API_TOKEN - The API token set as an env-variable in Orbi
*/
const ORBI = {
URL: process.env.ORBI ?? 'https://localhost:6970',
API_TOKEN: process.env.ORBI_API_TOKEN,
};

const config = {
PORT: parseInt(process.env.PORT ?? '3000', 10),
HOST: process.env.HOST ?? '0.0.0.0',
Expand All @@ -60,6 +70,7 @@ const config = {
LU,
WIKI,
JWT,
ORBI,
};

export default config;
4 changes: 4 additions & 0 deletions src/resolvers/activity.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { hasAccess, hasAuthenticated } from '@/util';
import { ActivityAPI } from '@api/activity';
import { Feature, Resolvers } from '@generated/graphql';
import { activityReducer } from '@reducer/activity';
import { updateOrbiActivities } from '@service/orbi';

const activityApi = new ActivityAPI();

Expand All @@ -16,6 +17,9 @@ const activityresolver: Resolvers = {
},
activities: async (_, { from, to, utskott }, ctx) => {
await hasAuthenticated(ctx);
//async call to update orbi because it is
//painfully slow
updateOrbiActivities();
const a = await activityApi.getActivities(from, to, utskott);

return reduce(a, activityReducer);
Expand Down
61 changes: 61 additions & 0 deletions src/services/orbi.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import prisma from '@/api/prisma';
import config from '@/config';
import { ServerError } from '@/errors/request.errors';
import { PrismaActivity, PrismaTicket } from '@prisma/client';
import axios from 'axios';

/*
Creates an axios-instance and sets the baseUrl and authorization header
to the corresponding values in the config
*/

const {
ORBI: { URL, API_TOKEN },
} = config;

const api = axios.create({
baseURL: URL,
headers: { 'x-api-key': API_TOKEN },
});

type OrbiResponse = [
{
activity: PrismaActivity;
tickets: [PrismaTicket];
departmentName: string;
},
];

let latestCall = 0;

export const updateOrbiActivities = async () => {
const timestamp = Date.now();
//If latest call was < 10 minutes ago
if (timestamp - latestCall < 600000) {
return;
}
try {
const res = await api.get<OrbiResponse>('/activities', { params: { timestamp } });

const promises = res.data.map(async (item) => {
const act = item.activity;

//Since the API only returns numbers for dates
act.startDate = new Date(act.startDate);
if (act.endDate) {
act.endDate = new Date(act.endDate);
}

await prisma.prismaActivity.upsert({
where: { id: act.id },
update: act,
create: act,
});
});

await Promise.all(promises);
} catch {
throw new ServerError('Det gick inte att updatera aktiviteter från Orbi');
}
latestCall = timestamp;
};

0 comments on commit 009b0bb

Please sign in to comment.