diff --git a/src/posts/enums/tag.enum.ts b/src/posts/enums/tag.enum.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b95ae41bf996f7e1f5614333f5973b905a26d29 --- /dev/null +++ b/src/posts/enums/tag.enum.ts @@ -0,0 +1,10 @@ +export enum TagEnum { + aLaUne = 'a-la-une', + appels = 'appels', + projets = 'projets', + formations = 'formations', + infos = 'infos', + dossiers = 'dossiers', + etudes = 'etudes', + ressources = 'ressources', +} diff --git a/src/posts/posts.controller.ts b/src/posts/posts.controller.ts index dbac88f835e0fb7ef5003369ddead24b3d86230f..dcc6aecfdf61bea0931f196d5062355690b0454d 100644 --- a/src/posts/posts.controller.ts +++ b/src/posts/posts.controller.ts @@ -1,6 +1,6 @@ -import { Controller, Get, HttpService, Param, Query } from '@nestjs/common'; +import { Controller, Get, HttpService, Logger, Param, Query } from '@nestjs/common'; import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { catchError, map } from 'rxjs/operators'; import { ApiQuery } from '@nestjs/swagger'; import { Post } from './schemas/post.schema'; import { PostsService } from './posts.service'; @@ -32,7 +32,24 @@ export class PostsController { page: query.page ? query.page : null, }, }) - .pipe(map((response) => response.data)); + .pipe( + map((response) => response.data), + catchError((err) => { + Logger.error(err); + return new Observable(); + }) + ); + } + + @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] }; + }); } @Get(':id') @@ -46,14 +63,4 @@ export class PostsController { }) .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] }; - }); - } } diff --git a/src/posts/posts.service.ts b/src/posts/posts.service.ts index 469a95dad41a113fa7292554e77b95c31dfead16..4769e1f8a186fd7b195c9f34253a5ff6a177cd61 100644 --- a/src/posts/posts.service.ts +++ b/src/posts/posts.service.ts @@ -3,6 +3,7 @@ import { Logger } from '@nestjs/common'; import * as GhostAdminAPI from '@tryghost/admin-api'; import * as _ from 'lodash'; import { map } from 'rxjs/operators'; +import { TagEnum } from './enums/tag.enum'; import { Tag } from './schemas/tag.schema'; @Injectable() @@ -27,6 +28,24 @@ export class PostsService { public getTags(): Promise<Tag[]> { return this.api.tags.browse({ limit: 'all' }); } + // public getTags(): Promise<Tag[]> { + // return new Promise((resolve) => { + // this.api.tags.browse({ limit: 'all' }).then((tags: Tag[]) => { + // tags.sort((x, y) => { + // if (x < y) { + // return -1; + // } + // if (x > y) { + // return 1; + // } + // return 0; + // }); + + // console.log(tags); + // resolve(tags); + // }); + // }); + // } /** * Get all locations tags. Ex: Oullins, Sainte-foy etc... @@ -49,6 +68,20 @@ export class PostsService { */ public async getRegularTags(): Promise<Tag[]> { const tags = await this.getTags(); - return _.filter(tags, { description: null }); + let publicTags = _.filter(tags, { description: null }); + // Remove 'Appel a projet' + _.remove(publicTags, { slug: TagEnum.appels }); + // Move 'a la une' at index 0 + publicTags = this.arraymove(publicTags, _.findIndex(publicTags, { slug: TagEnum.aLaUne }), 0); + // Move 'info' at index 1 + publicTags = this.arraymove(publicTags, _.findIndex(publicTags, { slug: TagEnum.infos }), 1); + return publicTags; + } + + public arraymove(arr, fromIndex, toIndex): Array<any> { + const element = arr[fromIndex]; + arr.splice(fromIndex, 1); + arr.splice(toIndex, 0, element); + return arr; } }