All files / server/articles createArticle.ts

0% Statements 0/24
100% Branches 0/0
100% Functions 0/0
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                                                         
import * as moment from 'moment';
import { ArticleStatuses } from '../../shared/ArticleStatuses';
import { IArticleCreate } from '../../shared/types/article';
import upload from '../api/s3';
import { articleModel } from '../model/article';
import { getCategory } from '../model/category';
import { slugify } from '../utils';
 
export default async ({
  title,
  category: categoryId,
  text,
  type,
  userSub,
  username,
  image,
}: IArticleCreate) => {
  const { createReadStream } = await image;
  const stream = createReadStream();
  const uploaded = await upload(stream);
  const slug = slugify(title);
  const category = await getCategory(categoryId);
  const user = { username, sub: userSub };
 
  return articleModel.create({
    title,
    slug,
    category,
    text,
    type,
    user,
    image: uploaded.Location,
    createdDateTime: moment(),
    status: ArticleStatuses.new,
  });
};