Skip to content
Snippets Groups Projects
Commit ed1fa267 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

feat: add tag and posts db init

parent 60ed843b
No related branches found
No related tags found
3 merge requests!46Recette,!45Dev,!40Feat/news tags
scripts/data/etude.png

17.9 KiB

scripts/data/folder.png

12.9 KiB

scripts/data/la_carniere.png

13 KiB

scripts/data/nec.png

133 KiB

This diff is collapsed.
{
"tags": [
{
"id": "601913f3eed9fb0001b487a5",
"name": "Oullins",
"slug": "oullins",
"description": "commune",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-02T08:57:23.000Z",
"updated_at": "2021-02-26T14:52:32.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390ae08cf07e0001358e5c",
"name": "À la une",
"slug": "a-la-une",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:51:12.000Z",
"updated_at": "2021-02-26T14:51:12.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390af58cf07e0001358e5e",
"name": "Infos",
"slug": "infos",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:51:33.000Z",
"updated_at": "2021-02-26T14:51:33.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390aff8cf07e0001358e60",
"name": "Projets",
"slug": "projets",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:51:43.000Z",
"updated_at": "2021-02-26T14:51:43.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b0a8cf07e0001358e62",
"name": "Ressources",
"slug": "ressources",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:51:54.000Z",
"updated_at": "2021-02-26T14:51:54.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b138cf07e0001358e64",
"name": "Formations",
"slug": "formations",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:52:03.000Z",
"updated_at": "2021-02-26T14:52:03.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b208cf07e0001358e66",
"name": "Études",
"slug": "etudes",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:52:16.000Z",
"updated_at": "2021-02-26T14:52:16.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b288cf07e0001358e68",
"name": "Dossiers",
"slug": "dossiers",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:52:24.000Z",
"updated_at": "2021-02-26T14:52:24.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b4e8cf07e0001358e6b",
"name": "Tout public",
"slug": "tout-public",
"description": "public",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:53:02.000Z",
"updated_at": "2021-02-26T14:53:02.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b6f8cf07e0001358e6d",
"name": "Moins de 16 ans",
"slug": "moins-de-16-ans",
"description": "public",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:53:35.000Z",
"updated_at": "2021-02-26T14:53:35.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b808cf07e0001358e6f",
"name": "Jeunes (16-25ans)",
"slug": "jeunes-16-25ans",
"description": "public",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:53:52.000Z",
"updated_at": "2021-02-26T14:53:52.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390b988cf07e0001358e71",
"name": "Séniors (+ de 65ans)",
"slug": "seniors-de-65ans",
"description": "public",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:54:16.000Z",
"updated_at": "2021-02-26T14:54:16.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390ba58cf07e0001358e73",
"name": "Handicapés",
"slug": "handicapes",
"description": "public",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:54:29.000Z",
"updated_at": "2021-02-26T14:54:29.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390bb98cf07e0001358e75",
"name": "Allophones",
"slug": "allophones",
"description": "public",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:54:49.000Z",
"updated_at": "2021-02-26T14:54:49.000Z",
"url": "http://localhost:2368/404/"
},
{
"id": "60390c568cf07e0001358e77",
"name": "Appels à projets",
"slug": "appels",
"description": null,
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"meta_title": null,
"meta_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-02-26T14:57:26.000Z",
"updated_at": "2021-02-26T14:57:26.000Z",
"url": "http://localhost:2368/404/"
}
]
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const _ = require('lodash');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tagsData = require('./ghost/migrations/init/tags.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const postsData = require('./ghost/migrations/init/posts.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
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({
url: process.env.GHOST_HOST_AND_PORT,
key: process.env.GHOST_ADMIN_API_KEY,
version: 'v3',
});
async function deleteTags(existingTags) {
return await Promise.all(
_.forEach(existingTags, async (tag) => {
api.tags
.delete(_.pick(tag, ['id']))
.then((res) => {
return null;
})
.catch((error) => console.error(error));
})
);
}
async function deletePosts(existingPosts) {
return await Promise.all(
_.forEach(existingPosts, async (tag) => {
api.posts
.delete(_.pick(tag, ['id']))
.then((res) => {
return null;
})
.catch((error) => console.error(error));
})
);
}
async function createTags(deleteOnly) {
// Get existing tags
await api.tags
.browse({ limit: 'all' })
.then(async (existingTags) => {
// remove 'meta' key
delete existingTags['meta'];
if (existingTags.length > 0) {
console.log('-- Dropping ' + existingTags.length + ' tags... --');
// Delete existing tags
await deleteTags(existingTags).then(() => {
console.log('-- Tags dropped --');
});
} else {
console.log('-- No tag to drop --');
}
// Creating new tags
if (!deleteOnly) {
console.log('-- Creating ' + tagsData.tags.length + ' tags --');
_.forEach(tagsData.tags, (tag) => {
api.tags
.add(tag)
.then((res) => {
console.log('-- Tag `' + res.name + '` created --');
})
.catch((error) => console.error(error));
});
}
})
.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 = [];
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 createPosts(deleteOnly) {
// Get existing posts
api.posts
.browse({ limit: 'all' })
.then(async (existingPosts) => {
// remove 'meta' key
delete existingPosts['meta'];
if (existingPosts.length > 0) {
console.log('-- Dropping ' + existingPosts.length + ' posts... --');
// Delete existing posts
await deletePosts(existingPosts).then(() => {
console.log('-- Posts dropped --');
});
} else {
console.log('-- No posts to drop --');
}
// Creating new posts
if (!deleteOnly) {
console.log('-- Creating ' + postsData.length + ' posts --');
_.forEach(postsData, (post) => {
api.posts
.add(post, { source: 'html' })
.then((res) => {
console.log('-- Post `' + res.title + '` created --');
})
.catch((error) => console.error(error));
});
}
})
.catch((error) => console.error(error));
}
async function main(deleteOnly) {
createTags(deleteOnly).then(() => {
createPosts(deleteOnly);
});
}
var myArgs = process.argv.slice(2);
switch (myArgs[0]) {
case 'drop':
console.log('-- Droping data --');
main(true);
break;
case 'up':
console.log('-- Init db --');
main(false);
break;
default:
console.error('Unknown cmd');
}
...@@ -3,10 +3,12 @@ import { Observable } from 'rxjs'; ...@@ -3,10 +3,12 @@ import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { ApiQuery } from '@nestjs/swagger'; import { ApiQuery } from '@nestjs/swagger';
import { Post } from './schemas/post.schema'; import { Post } from './schemas/post.schema';
import { PostsService } from './posts.service';
import { Tag } from './schemas/tag.schema';
@Controller('posts') @Controller('posts')
export class PostsController { export class PostsController {
constructor(private readonly httpService: HttpService) {} constructor(private readonly httpService: HttpService, private readonly postsService: PostsService) {}
@Get() @Get()
@ApiQuery({ name: 'include', type: String, required: false }) @ApiQuery({ name: 'include', type: String, required: false })
...@@ -32,4 +34,15 @@ export class PostsController { ...@@ -32,4 +34,15 @@ export class PostsController {
}) })
.pipe(map((response) => response.data)); .pipe(map((response) => response.data));
} }
@Get('tags')
public async findAllTags(): Promise<{ public: Tag[]; commune: Tag[]; others: Tag[] }> {
return Promise.all([
this.postsService.getLocationTags(),
this.postsService.getPublicTags(),
this.postsService.getRegularTags(),
]).then((data) => {
return { commune: data[0], public: data[1], others: data[2] };
});
}
} }
import { HttpService, Injectable } from '@nestjs/common'; import { HttpService, Injectable } from '@nestjs/common';
import { Logger } from '@nestjs/common';
import * as GhostAdminAPI from '@tryghost/admin-api'; import * as GhostAdminAPI from '@tryghost/admin-api';
import * as _ from 'lodash';
import { map } from 'rxjs/operators';
import { Tag } from './schemas/tag.schema';
@Injectable() @Injectable()
export class PostsService { export class PostsService {
// ID's use in ghost tag description in order to categories tags.
public readonly locationCategory = 'commune';
public readonly publicCategory = 'public';
private api: any; private api: any;
constructor(private readonly httpService: HttpService) { constructor() {
// Configure Ghost client // Configure Ghost client
this.api = new GhostAdminAPI({ this.api = new GhostAdminAPI({
url: process.env.GHOST_HOST_AND_PORT, url: process.env.GHOST_HOST_AND_PORT,
// Admin API key goes here key: process.env.GHOST_ADMIN_API_KEY,
key: process.env.GHOST_HOST_AND_PORT,
version: 'v3', version: 'v3',
}); });
} }
/**
* Get all tags with admin endpoint, including unused ones
*/
public getTags(): Promise<Tag[]> {
return this.api.tags.browse({ limit: 'all' });
}
/**
* Get all locations tags. Ex: Oullins, Sainte-foy etc...
*/
public async getLocationTags(): Promise<Tag[]> {
const tags = await this.getTags();
return _.filter(tags, { description: this.locationCategory });
}
/**
* Get all public tags. Ex: Moins de 16 ans, adultes...
*/
public async getPublicTags(): Promise<Tag[]> {
const tags = await this.getTags();
return _.filter(tags, { description: this.publicCategory });
}
/**
* Get other tags. Ex: A la une, infos...
*/
public async getRegularTags(): Promise<Tag[]> {
const tags = await this.getTags();
return _.filter(tags, { description: null });
}
} }
export class Tag {
id: string;
name: string;
slug: string;
description: string;
feature_image: null;
visibility: string;
og_image: string;
og_title: string;
og_description: string;
twitter_image: string;
twitter_title: string;
twitter_description: string;
meta_title: string;
meta_description: string;
codeinjection_head: string;
codeinjection_foot: string;
canonical_url: string;
accent_color: string;
created_at: string;
updated_at: string;
url: string;
}
...@@ -11,9 +11,12 @@ ME_CONFIG_BASICAUTH_USERNAME=<mongo express username> ...@@ -11,9 +11,12 @@ ME_CONFIG_BASICAUTH_USERNAME=<mongo express username>
ME_CONFIG_BASICAUTH_PASSWORD=<mongo express password> ME_CONFIG_BASICAUTH_PASSWORD=<mongo express password>
ME_PORT=<mongo express port> ME_PORT=<mongo express port>
SALT=<Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue> SALT=<Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue>
MAIL_URL=<API url> MAIL_URL=<SEN API url>
MAIL_TOKEN=<API token> MAIL_TOKEN=<SEN API token>
APTIC_TOKEN=<APTIC API TOKEN> APTIC_TOKEN=<APTIC API TOKEN>
GHOST_PORT=<ghost port> GHOST_PORT=<ghost port>
GHOST_DB_PASSWORD=<ghost db password> GHOST_DB_PASSWORD=<ghost db password>
USER_PWD=<test user password> GHOST_CONTENT_API_KEY=<Ghost global api key, can be found in integration part of ghost UI>
GHOST_ADMIN_API_KEY=<Ghost admin api key, can be found in integration part of ghost UI>
GHOST_HOST_AND_PORT=<Ghost host and port, ex:http://localhost:2368>
USER_PWD=<test user password, this password will be user by every test users>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment