Skip to content
Snippets Groups Projects
posts.service.ts 2.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    import { Injectable } from '@nestjs/common';
    
    import * as GhostAdminAPI from '@tryghost/admin-api';
    
    import * as _ from 'lodash';
    
    import { ConfigurationService } from '../configuration/configuration.service';
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { rewriteGhostImgUrl } from '../shared/utils';
    
    import { TagEnum } from './enums/tag.enum';
    
    import { Post } from './schemas/post.schema';
    
    import { Tag } from './schemas/tag.schema';
    
    
    @Injectable()
    export class PostsService {
    
      // ID's use in ghost tag description in order to categories tags.
      public readonly locationCategory = 'commune';
      public readonly publicCategory = 'public';
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      private api;
    
      constructor(private readonly configService: ConfigurationService) {
    
        // Configure Ghost client
        this.api = new GhostAdminAPI({
          url: process.env.GHOST_HOST_AND_PORT,
    
          key: process.env.GHOST_ADMIN_API_KEY,
    
          version: 'v5.0',
    
    
      /**
       * 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();
    
        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;
      }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      public arraymove(arr, fromIndex, toIndex): Array<unknown> {
    
        const element = arr[fromIndex];
        arr.splice(fromIndex, 1);
        arr.splice(toIndex, 0, element);
        return arr;
    
    
      public formatPosts(postData: Post): Post {
        // Handle excerpt field in case of no author
        if (!postData.custom_excerpt) {
          postData.excerpt = 'Inconnu';
        }
    
        // Handle image display. Rewrite image URL to fit ghost infra issue.
    
        postData = rewriteGhostImgUrl(this.configService, postData);
    
        return postData;
      }