Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/addCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Server } from '../src/server';
import * as categories from './categories.json';
import { Category } from '../src/apps/categories/categories.entity';
import * as _ from 'lodash';
import { Connection } from 'typeorm/index';

let conn: Connection;


const main = async () => {
conn = await Server.connectToDB();
await recursive(categories,null);
await conn.close();
}

const insertIntoDb = async(name:string,code:string,representation:string,parent:any )=>{
const newCategory=new Category();
newCategory.name=name;
newCategory.code=code;
newCategory.representation=representation;
newCategory.parent=parent;
try {
await newCategory.save();
} catch (error) {
console.log(error);
}
}

const recursive = async(level:any[],parent:any) =>{
level.map(async(cat)=>{
let representation=cat.representation;
representation=_.kebabCase(representation);
await insertIntoDb(cat.name, cat.code, representation,parent);
await recursive(cat.child,cat);
});
}


main().then(async()=>{
console.log('Categories Inserted');
}).catch(()=>{
console.log('Categories not inserted');
});
1 change: 1 addition & 0 deletions lib/categories.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"db:production": "NODE_PATH=src node ./node_modules/typeorm/cli.js --config ormconfig.js",
"gen-schema-types": "NODE_PATH=src ts-node ./lib/generateGQLTypes.ts",
"lint": "eslint '*/**/*.{js,ts,json}' --fix",
"makemigrations": "yarn db migration:generate",
"makemigrations": "yarn db:development migration:generate",
"add-categories": "NODE_PATH=src ts-node ./lib/addCategories.ts",
"migrate": "yarn db migration:run",
"start": "per-env",
"start:development": "nodemon --exec ts-node src/index.ts",
Expand Down
39 changes: 39 additions & 0 deletions src/apps/categories/addCategory/addCategory.resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ResolverMap ,IExceptions} from 'types';
import {Category} from '../categories.entity';
import { User } from '../../user.entity';
import { Exceptions } from '../../../helpers/exceptions';
import { categoryArgsValidator } from './validators';
import { DUPLICATE_REPRESENTATION } from '../exceptions';



const Resolvers: ResolverMap = {
CategoryOrException:{
__resolveType: (obj): string => (obj.exceptions ? 'Exceptions' : 'Category'),
},
Mutation: {
addCategory: async(_,args,{ session }): Promise<Category | IExceptions> =>{
// const user = await User.authenticationRequired(session);
const { data, exceptions, push, validate } = new Exceptions(args);

if (!await validate(categoryArgsValidator, args)) return exceptions;

const { name, code,representation } = data;

const newCategory=new Category();
newCategory.name=name;
newCategory.code=code;
newCategory.representation=representation;

try {
await newCategory.save();
} catch (error) {
return push(DUPLICATE_REPRESENTATION({ data: { representation: newCategory.representation } }))
}
console.log(newCategory);
return newCategory;
}
},
};

export default Resolvers;
9 changes: 9 additions & 0 deletions src/apps/categories/addCategory/addCategory.schema.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import Category from "apps/categories/categories.schema.gql";
type Mutation {
addCategory(
name: String!,
code: String!,
representation:String,
parent:Category
):Category
}
9 changes: 9 additions & 0 deletions src/apps/categories/addCategory/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as yup from 'yup';
import * as _ from 'lodash';


export const categoryArgsValidator = yup.object().shape({
name: yup.string().required('Name is required for the category.'),
code: yup.string().required('Category needs a unique code.'),
representation: yup.string().transform(value => _.kebabCase(value))
});
27 changes: 27 additions & 0 deletions src/apps/categories/categories.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Entity, Column ,ManyToOne } from 'typeorm';
import { BaseEntity } from 'helpers/db';
import * as _ from 'lodash';

@Entity('category')
export class Category extends BaseEntity {
@Column()
name:string;

@Column()
code:string;

@Column({nullable:false,unique:true})
representation:string;

@ManyToOne(type => Category, parent => parent.parent)
parent: Category;

get _representation(){
return this.representation;
}

set _representation(value){
this.representation=_.kebabCase(value);
}

}
13 changes: 13 additions & 0 deletions src/apps/categories/categories.resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ResolverMap } from 'types';
import { Category } from './categories.entity';
const Resolvers: ResolverMap = {
Query: {
categories: async (_, { representation }:GQL.fetchCategories,): Promise<Category[] | null> => {
const baseCategories= await Category.find();
return baseCategories;

},
},
};

export default Resolvers;
14 changes: 14 additions & 0 deletions src/apps/categories/categories.schema.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# import Exceptions from 'apps/schema.gql'

type Category{
name:String,
code:String,
representation:String,
parent:Category
}

union CategoryOrException = Category | Exception

type Query{
categories(representation:String):[Category]
}
66 changes: 66 additions & 0 deletions src/apps/categories/categories.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Server } from 'server';
import { Connection } from 'typeorm';
import { TestClient } from '../../server/client';
import { Category } from './categories.entity';

let conn: Connection;
beforeAll(async () => {
conn = await Server.connectToDB();
});

afterAll(async () => {
await conn.close();
});

const $categories = (representation:string) => `
query{
categories(representation:"${representation}"){
name
code
representation
}
}
`;

const $addCategory = (name: string, code: string, representation: string,) => `
mutation {
addCategory(name: "${name}",code:"${code}",representation:"${representation}"){
name
code
representation
}
}
`;


describe('categories',()=>{
test('fetch base categories', async () => {
const client = new TestClient();
const count =await Category.count();
const {categories} = await client.query($categories(null));
expect(count).toEqual(categories.length);
});
test('fetch filtered categories', async () => {
const client = new TestClient();
const count =await Category.count();
const {categories} = await client.query($categories('base'));
expect(count).toEqual(categories.length);
categories.map((cat)=>{
expect(cat.parent).toEqual('base');
});
});
test('adding category',async()=>{
const client = new TestClient();
const newCategory = new Category()
newCategory.name = 'testCategory'
newCategory.code='po789'
newCategory.representation='/testCategory'
await newCategory.save()
console.log(newCategory);
const { addCategory }= await client.query($addCategory(newCategory.name,newCategory.code,newCategory.representation));
console.log(addCategory);
const {name}=addCategory;
expect(name).toEqual(newCategory.name);
})

})
11 changes: 11 additions & 0 deletions src/apps/categories/exceptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Exceptions } from 'helpers/exceptions';

export const DUPLICATE_REPRESENTATION = Exceptions.generator({
code: 'DUPLICATE_REPRESENTATION',
message: 'Category of same representation already exists',
});

export const INVALID_ARGUMENTS_RECEIVED = Exceptions.generator({
code: 'INVALID_ARGUMENTS_TYPE',
message: 'Expected a valid Representation and a name and code for category',
});
28 changes: 0 additions & 28 deletions src/migrations/1597786152241-InitialMigration.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/types/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ declare namespace GQL {
mimetype: string;
encoding: string;
}
interface AddCategoryMutationArguments{
name:string;
code:string;
representation:string;
parent:string;
}
interface fetchCategories{
representation:string;
}


type DoneOrExceptions = IDone | IExceptions;
}
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

{
"compilerOptions": {
"baseUrl": "./src",
Expand All @@ -7,6 +8,7 @@
"esnext",
"dom"
],
"resolveJsonModule": true,
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
Expand Down