Skip to content

Commit

Permalink
Merge pull request #139 from HannesOberreiter/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
HannesOberreiter committed Dec 18, 2023
2 parents 2c46211 + ca30e7d commit d3eba72
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 384 deletions.
4 changes: 2 additions & 2 deletions mails/partials/footer_en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Reisstraße 17, 8741 Weißkirchen in der Steiermark, AUSTRIA
This E-Mail was send to %mail%, from %base_url% because it is an important e-mail to use the application or you opt-in to get automated information mails. As for the information mails, you can turn them off on your profile page or with the unsubscribe link bellow. We do not send Newsletter. This is an automated system email. Please do not reply to this email.

- Unsubscribe: %base_url%visitor/unsubscribe
- Privacy Policy: https://www.btree.at/%lang%/data-privacy
- Legal Information: https://www.btree.at/%lang%/impressum
- Privacy Policy: https://www.btree.at/data-privacy
- Legal Information: https://www.btree.at/impressum
4 changes: 2 additions & 2 deletions mails/partials/footer_fr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Reisstraße 17, 8741 Weißkirchen in der Steiermark, AUSTRIA
This E-Mail was send to %mail%, from %base_url% because it is an important e-mail to use the application or you opt-in to get automated information mails. As for the information mails, you can turn them off on your profile page or with the unsubscribe link bellow. We do not send Newsletter. This is an automated system email. Please do not reply to this email.

- Unsubscribe: %base_url%visitor/unsubscribe
- Privacy Policy: https://www.btree.at/%lang%/data-privacy
- Legal Information: https://www.btree.at/%lang%/impressum
- Privacy Policy: https://www.btree.at/data-privacy
- Legal Information: https://www.btree.at/impressum
4 changes: 2 additions & 2 deletions mails/partials/footer_it.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Reisstraße 17, 8741 Weißkirchen in der Steiermark, AUSTRIA
This E-Mail was send to %mail%, from %base_url% because it is an important e-mail to use the application or you opt-in to get automated information mails. As for the information mails, you can turn them off on your profile page or with the unsubscribe link bellow. We do not send Newsletter. This is an automated system email. Please do not reply to this email.

- Unsubscribe: %base_url%visitor/unsubscribe
- Privacy Policy: https://www.btree.at/%lang%/data-privacy
- Legal Information: https://www.btree.at/%lang%/impressum
- Privacy Policy: https://www.btree.at/data-privacy
- Legal Information: https://www.btree.at/impressum
806 changes: 445 additions & 361 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@fastify/compress": "^6.4.0",
"@fastify/cookie": "^9.0.0",
"@fastify/helmet": "^11.0.0",
"@fastify/rate-limit": "^8.0.3",
"@fastify/rate-limit": "^9.0.1",
"@fastify/session": "^10.4.0",
"ajv-formats": "^2.1.1",
"archiver": "^6.0.0",
Expand All @@ -50,9 +50,9 @@
"google-auth-library": "^9.0.0",
"gpt-3-encoder": "^1.1.4",
"http-errors": "^2.0.0",
"ical-generator": "^5.0.0",
"ical-generator": "^6.0.0",
"ioredis": "^5.3.1",
"knex": "^2.3.0",
"knex": "^3.1.0",
"lodash-es": "^4.17.21",
"mysql": "^2.18.1",
"node-schedule": "^2.1.0",
Expand All @@ -64,11 +64,11 @@
"proj4": "^2.9.0",
"query-string": "^8.1.0",
"rotating-file-stream": "^3.0.4",
"stripe": "^13.2.0",
"stripe": "^14.8.0",
"ua-parser-js": "^1.0.35"
},
"devDependencies": {
"@types/archiver": "^5.3.1",
"@types/archiver": "^6.0.2",
"@types/lodash": "^4.14.191",
"@types/node": "^20.3.1",
"@types/node-schedule": "^2.1.0",
Expand Down
8 changes: 4 additions & 4 deletions src/api/controllers/external.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export default class ExternalController {
url: 'https://app.btree.at/',
});
}
calendar.serve(
reply.raw,
`btree-${params.source}-${new Date().toISOString()}.ics`,
);
const filename = `btree-${params.source}-${new Date().toISOString()}.ics`;
reply.header('Content-Disposition', `attachment; filename=${filename}`);
reply.header('Content-Type', 'text/calendar; charset=utf-8');
return calendar.toString();
}

/**
Expand Down
40 changes: 33 additions & 7 deletions src/api/controllers/public.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export default class PublicController {
return cached;
}

const query = Observation.query().select(
'location',
raw('JSON_EXTRACT(data, "$.uri") as uri'),
'observed_at',
);

const start = new Date(Date.now() - 1000 * 60 * 60 * 24 * 182);
const end = new Date(Date.now());
const result = await Observation.query()
.select(
'location',
raw('JSON_EXTRACT(data, "$.uri") as uri'),
'observed_at',
)
.whereBetween('observed_at', [start, end]);
query.whereBetween('observed_at', [start, end]);

const result = await query;

redis.set(
'cache:velutinaObservationsRecent',
Expand All @@ -36,6 +38,30 @@ export default class PublicController {
return result;
}

static async getVelutinaObservationsYear(
req: FastifyRequest,
reply: FastifyReply,
) {
reply.header('Cache-Control', 'public, max-age=21600');
const querystring = req.params as any;
if (!querystring.year) {
return [];
}
const year = querystring.year;

const query = Observation.query()
.select(
'location',
raw('JSON_EXTRACT(data, "$.uri") as uri'),
'observed_at',
)
.where(raw('YEAR(observed_at)'), year);

const result = await query;

return result;
}

static async getVelutinaObservationsStats(
_req: FastifyRequest,
reply: FastifyReply,
Expand Down
7 changes: 7 additions & 0 deletions src/api/routes/v1/auth.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import AuthController from '../../controllers/auth.controller.js';
import { Guard } from '../../hooks/guard.hook.js';
import { ROLES } from '../../../config/constants.config.js';
import { GoogleAuth } from '../../../services/federated.service.js';
import RootController from '../../controllers/root.controller.js';

export default function routes(
instance: FastifyInstance,
Expand Down Expand Up @@ -126,5 +127,11 @@ export default function routes(
AuthController.google,
);

server.get(
'/ping',
{ preHandler: Guard.authorize([ROLES.read, ROLES.admin, ROLES.user]) },
RootController.status,
);

done();
}
2 changes: 2 additions & 0 deletions src/api/routes/v1/hive.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const hiveSchema = z.object({
modus: z.boolean().optional(),
modus_date: z.string().optional(),
deleted: z.boolean().optional(),
source_id: z.number().int().optional(),
type_id: z.number().int().optional(),
});

export default function routes(
Expand Down
15 changes: 15 additions & 0 deletions src/api/routes/v1/public.route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FastifyInstance } from 'fastify';
import { ZodTypeProvider } from 'fastify-type-provider-zod';
import PublicController from '../../controllers/public.controller.js';
import { z } from 'zod';
import { numberSchema } from '../../utils/zod.util.js';

export default function routes(
instance: FastifyInstance,
Expand All @@ -14,6 +16,19 @@ export default function routes(
{},
PublicController.getVelutinaObservationsRecent,
);

server.get(
'/velutina/observations/year/:year',
{
schema: {
params: z.object({
year: numberSchema,
}),
},
},
PublicController.getVelutinaObservationsYear,
);

server.get(
'/velutina/observations/stats',
{},
Expand Down
2 changes: 1 addition & 1 deletion src/api/utils/stripe.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function createOrder(
quantity: number,
) {
const stripe = new Stripe(stripeSecret, {
apiVersion: '2023-08-16',
apiVersion: '2023-10-16',
typescript: true,
});
const session = await stripe.checkout.sessions.create({
Expand Down
59 changes: 59 additions & 0 deletions src/api/utils/velutina.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export async function fetchObservations() {
const observationOrg = await fetchObservationOrg();
const patriNat = await fetchPatriNat();
const artenfinderNet = await fetchArtenfinderNet();
const infoFaunaCh = await fetchInfoFaunaCh();

return {
iNaturalist: inat,
patriNat: patriNat,
artenfinderNet: artenfinderNet,
observationOrg: observationOrg,
infoFaunaCh: infoFaunaCh,
};
}

Expand Down Expand Up @@ -273,3 +275,60 @@ export async function fetchObservationOrg() {
}
return { newObservations: newObservations };
}

export async function fetchInfoFaunaCh() {
const url =
'https://api.gbif.org/v1/occurrence/search?dataset_key=81981d98-e27f-4155-9a94-9eae9bbad2be&taxon_key=1311477';

let endOfRecords = false;
let offset = 0;
let newObservations = 0;
const limit = 300;
let yearFilter = `&year=${
new Date().getFullYear() - 1
},${new Date().getFullYear()}`;
const oldRecords = await Observation.query()
.select('external_id')
.where('external_service', 'Info Fauna (GBIF)');
const searchArray = oldRecords.map((o) => o.external_id);

if (searchArray.length === 0) yearFilter = '';

while (endOfRecords === false) {
const result = await fetch(
`${url}&limit=${limit}&offset=${offset}${yearFilter}`,
);
const data = await result.json();
if (data.results.length === 0) break;
endOfRecords = data.endOfRecords;
offset += limit;

const observations = [];
const results = data.results;
for (let i = 0; i < results.length; i++) {
if (searchArray.includes(Number(results[i]['gbifID']))) {
continue;
}
newObservations++;
const observation = results[i];
observations.push({
external_id: Number(observation['gbifID']),
external_uuid: observation['catalogNumber'],
external_service: 'Info Fauna (GBIF)',
observed_at: observation['eventDate'],
location: {
lat: Number(observation['decimalLatitude']),
lng: Number(observation['decimalLongitude']),
},
taxa: 'Vespa velutina',
data: {
bibliographicCitation: observation['bibliographicCitation'],
identificationRemarks: observation['identificationRemarks'],
uri: 'https://lepus.infofauna.ch/carto/58510',
},
});
}
await Observation.query().insertGraph(observations);
}
return { newObservations: newObservations };
}

0 comments on commit d3eba72

Please sign in to comment.