diff --git a/scripts/init-ghost.js b/scripts/init-ghost.js index e17c8a2c8602e029cd8e1938d27e6f832b79d325..c8ffd47e2b3617e91f7ae9d84f0232dea914c454 100644 --- a/scripts/init-ghost.js +++ b/scripts/init-ghost.js @@ -14,18 +14,18 @@ const GhostAdminAPI = require('@tryghost/admin-api'); // eslint-disable-next-line @typescript-eslint/no-var-requires require('dotenv').config({ path: path.resolve(__dirname, '../.env') }); -var api = new GhostAdminAPI({ +const api = new GhostAdminAPI({ url: process.env.GHOST_HOST_AND_PORT, key: process.env.GHOST_ADMIN_API_KEY, version: 'v3', }); async function deleteTags(existingTags) { - return await Promise.all( + return Promise.all( _.forEach(existingTags, async (tag) => { await api.tags .delete(_.pick(tag, ['id'])) - .then((res) => { + .then(() => { return null; }) .catch((error) => console.error(error)); @@ -33,11 +33,11 @@ async function deleteTags(existingTags) { ); } async function deletePosts(existingPosts) { - return await Promise.all( + return Promise.all( _.forEach(existingPosts, async (tag) => { await api.posts .delete(_.pick(tag, ['id'])) - .then((res) => { + .then(() => { return null; }) .catch((error) => console.error(error)); @@ -45,11 +45,11 @@ async function deletePosts(existingPosts) { ); } async function deletePages(existingPages) { - return await Promise.all( + return Promise.all( _.forEach(existingPages, async (id) => { await api.pages .delete(_.pick(id, ['id'])) - .then((res) => { + .then(() => { return null; }) .catch((error) => console.error(error)); @@ -94,30 +94,6 @@ async function createTags(deleteOnly) { .catch((error) => console.error(error)); } -// Utility function to find and upload any images in an HTML string -function processImagesInHTML(html) { - // Find images that Ghost Upload supports - let imageRegex = /="([^"]*?(?:\.jpg|\.jpeg|\.gif|\.png|\.svg|\.sgvz))"/gim; - let imagePromises = []; - let result; - - while ((result = imageRegex.exec(html)) !== null) { - let file = result[1]; - // Upload the image, using the original matched filename as a reference - imagePromises.push( - api.images.upload({ - ref: file, - file: path.resolve(file), - }) - ); - } - - return Promise.all(imagePromises).then((images) => { - images.forEach((image) => (html = html.replace(image.ref, image.url))); - return html; - }); -} - async function uploadPostImage(imagePath) { let imagePromise = api.images.upload({ ref: imagePath, @@ -214,44 +190,6 @@ async function createPages(deleteOnly) { .catch((error) => console.error(error)); } -async function createPages(deleteOnly) { - api.pages - .browse({ limit: 'all' }) - .then(async (existingPages) => { - // remove 'meta' key - delete existingPages['meta']; - if (existingPages.length > 0) { - console.log(`-- Dropping ${existingPages.length} pages... --`); - // Delete existing pages - await deletePages(existingPages).then(() => { - console.log('-- Pages dropped --'); - }); - } else { - console.log('-- No pages to drop --'); - } - // wait complete delete of pages, if not page slugs are appended by _2 - await new Promise((r) => setTimeout(r, 1000)); - - // Creating new pages - if (!deleteOnly) { - console.log(`-- Creating ${pagesData.length} pages --`); - _.forEach(pagesData, async (page) => { - //upload de l'image en featured_image - if (page.feature_image) { - page.feature_image = await uploadPostImage(page.feature_image); - } - api.pages - .add(page, { source: 'html' }) - .then((res) => { - console.log(`-- Page \`${res.title}\` created --`); - }) - .catch((error) => console.error(error)); - }); - } - }) - .catch((error) => console.error(error)); -} - async function main(deleteOnly) { createTags(deleteOnly) .then(() => { @@ -262,7 +200,7 @@ async function main(deleteOnly) { }); } -var myArgs = process.argv.slice(2); +const myArgs = process.argv.slice(2); switch (myArgs[0]) { case 'drop': diff --git a/scripts/init-index.js b/scripts/init-index.js index 0ddd701395ae700a828aaefc51466f6c23bfbb85..9411f6258a4b63ee564b357849548133a161e628 100644 --- a/scripts/init-index.js +++ b/scripts/init-index.js @@ -1,5 +1,8 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires const axios = require('axios'); +// eslint-disable-next-line @typescript-eslint/no-var-requires const path = require('path'); +// eslint-disable-next-line @typescript-eslint/no-var-requires require('dotenv').config({ path: path.resolve(__dirname, '../.env') }); axios diff --git a/scripts/init-tcl-stop-points.js b/scripts/init-tcl-stop-points.js new file mode 100644 index 0000000000000000000000000000000000000000..1f228922c22cee5dca056023d5be9240a0482bde --- /dev/null +++ b/scripts/init-tcl-stop-points.js @@ -0,0 +1,30 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires +const axios = require('axios'); +// eslint-disable-next-line @typescript-eslint/no-var-requires +const path = require('path'); +// eslint-disable-next-line @typescript-eslint/no-var-requires +require('dotenv').config({ path: path.resolve(__dirname, '../.env') }); + +axios + .post('http://localhost:3000/api/auth/login', { + email: 'admin@admin.com', + password: process.env.USER_PWD, + }) + .then((res) => { + const config = { + headers: { Authorization: `Bearer ${res.data.accessToken}` }, + }; + axios + .get('http://localhost:3000/api/tcl/update', config) + .then((res) => { + console.log(`TCL stop points | statusCode: ${res.status}`); + }) + .catch((error) => { + console.error('Error in fetching TCL stop points'); + console.error(error); + }); + }) + .catch((error) => { + console.error('Error in auth'); + console.error(error); + }); diff --git a/src/migrations/migrations-utils/template.ts b/src/migrations/migrations-utils/template.ts new file mode 100644 index 0000000000000000000000000000000000000000..d3f00960db09db7b37081642dadc328fa876ce13 --- /dev/null +++ b/src/migrations/migrations-utils/template.ts @@ -0,0 +1,13 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + const db: Db = await getDb(); +}; + +export const down = async () => { + const db: Db = await getDb(); + /* + Code you downgrade script here! + */ +}; diff --git a/src/migrations/scripts/1665499906453-remove-night-buses.ts b/src/migrations/scripts/1665499906453-remove-night-buses.ts new file mode 100644 index 0000000000000000000000000000000000000000..e0584b9b96c1a654306597ada9ed372e41255b31 --- /dev/null +++ b/src/migrations/scripts/1665499906453-remove-night-buses.ts @@ -0,0 +1,15 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + const db: Db = await getDb(); + await db.collection('tclstoppoints').updateMany({}, { $pull: { busLines: { $in: ['PL1', 'PL2', 'PL3'] } } }); + await db.collection('tclstoppoints').deleteMany({ + $and: [{ tramLines: { $size: 0 } }, { subLines: { $size: 0 } }, { busLines: { $size: 0 } }], + }); + console.log(`Update done : removed night buses from 'tclstoppoints' collection`); +}; + +export const down = async () => { + console.log('No down migration implemented for : remove night buses'); +}; diff --git a/src/tcl/tclStopPoint.controller.ts b/src/tcl/tclStopPoint.controller.ts index 5127b71c9263c8e8f0e83f27956723efab4c93a1..73de374c58b4fd763b55820c66f89c1caf6a0e06 100644 --- a/src/tcl/tclStopPoint.controller.ts +++ b/src/tcl/tclStopPoint.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Logger, Post, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { Roles } from '../users/decorators/roles.decorator'; @@ -10,6 +10,7 @@ import { TclStopPointService } from './tclStopPoint.service'; @ApiTags('tcl') @Controller('tcl') export class TclStopPointController { + private readonly logger = new Logger(TclStopPointController.name); constructor(private tclStopPointService: TclStopPointService) {} @ApiOperation({ @@ -23,6 +24,7 @@ export class TclStopPointController { @UseGuards(JwtAuthGuard, RolesGuard) @Roles('admin') public updateStopPoints(): Promise<void> { + this.logger.debug('updateStopPoints'); return this.tclStopPointService.updateStopPoints(); } diff --git a/src/tcl/tclStopPoint.service.ts b/src/tcl/tclStopPoint.service.ts index 06f88320f8c3086fc6fece4cfd6595564ca82d43..4aa396793f2c6839e3c724f811ec41a3eeb76e6e 100644 --- a/src/tcl/tclStopPoint.service.ts +++ b/src/tcl/tclStopPoint.service.ts @@ -1,5 +1,5 @@ import { HttpService } from '@nestjs/axios'; -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { PgisCoord } from './interfaces/pgis.coord'; @@ -29,6 +29,7 @@ interface Lines { @Injectable() export class TclStopPointService { + private readonly logger = new Logger(TclStopPointService.name); private receivedStopPoints: any[]; private receivedBusLines: any[]; private receivedSubLines: any[]; @@ -43,6 +44,7 @@ export class TclStopPointService { * Clear 'tclstoppoint' and fill it with data from Data Grand Lyon */ public async updateStopPoints(): Promise<void> { + this.logger.debug('updateStopPoints'); await this.getUpdatedData(); const newStopPoints = await this.processReceivedStopPoints(this.receivedStopPoints);