Commit f88f4639 authored by Tsvetelina Yordanova's avatar Tsvetelina Yordanova
Browse files

feat: #80 edit promotion

parent b527be88
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -53,6 +53,12 @@ export interface IPromotionCreateObject extends DBCreateObject {
	 */
	product: Product;

	/**
	 * @type {string}
	 * @memberof IPromotionCreateObject
	 */
	productId?: string;

	/**
	 * @type {number}
	 * @memberof IPromotionCreateObject
@@ -74,6 +80,12 @@ export interface IPromotionCreateObject extends DBCreateObject {
	 * @memberof IPromotionCreateObject
	 */
	warehouse: IWarehouse;

	/**
	 * @type {string}
	 * @memberof IPromotionCreateObject
	 */
	warehouseId?: string;
}

export interface IPromotion extends IPromotionCreateObject, DBRawObject {
+13 −3
Original line number Diff line number Diff line
import { Resolver, Query, Mutation } from '@nestjs/graphql';
import { IPromotionCreateObject } from '@ever-platform/common/src/interfaces/IPromotion';
import { PromotionService } from '../../../services/products/PromotionService';
import Promotion from '@ever-platform/common/src/entities/Promotion';

@Resolver('Promotion')
export class PromotionResolver {
	constructor(private readonly _promotionService: PromotionService) {}

	@Query('promotions')
	async getPromotions() {
		return this._promotionService.getAllPromotions();
	async getPromotions(_context, { findInput }) {
		return this._promotionService.getAllPromotions(findInput);
	}

	@Mutation()
	@Mutation('createPromotion')
	async createPromotion(
		_,
		{ createInput }: { createInput: IPromotionCreateObject }
@@ -36,4 +37,13 @@ export class PromotionResolver {

		return this._promotionService.removeMultipleByIds(promotionsIds);
	}

	@Mutation('updatePromotion')
	async updatePromotion(
		_,
		{ id, updateInput }: { id; updateInput }
	): Promise<Promotion> {
		await this._promotionService.throwIfNotExists(id);
		return this._promotionService.update(id, updateInput);
	}
}
+11 −4
Original line number Diff line number Diff line
@@ -4,11 +4,13 @@ type Promotion {
	description: [TranslateType]
	promoPrice: Float
	warehouse: Warehouse
	product: Product
	warehouseId: String
	productId: String
	active: Boolean
	activeFrom: Date
	activeTo: Date
	image: String
	product: Product
	purchasesCount: Int
}

@@ -21,7 +23,7 @@ type Product {
	_id: String!
}

input PromotionCreateInput {
input PromotionInput {
	title: [TranslateInput]
	description: [TranslateInput]
	promoPrice: Float
@@ -34,6 +36,10 @@ input PromotionCreateInput {
	purchasesCount: Int
}

input PromotionsFindInput {
	warehouse: String
}

input WarehouseInput {
	_id: String
}
@@ -44,11 +50,12 @@ input TranslateInput {
}

type Query {
	promotions: [Promotion]
	promotions(findInput: PromotionsFindInput): [Promotion]
}

type Mutation {
	createPromotion(createInput: PromotionCreateInput): Promotion
	createPromotion(createInput: PromotionInput): Promotion
	updatePromotion(id: String, updateInput: PromotionInput): Promotion
	removePromotion(id: String!): Void
	removePromotionsByIds(ids: [String!]!): Remove
}
+27 −3
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import { createEverLogger } from '../../helpers/Log';
import Logger from 'bunyan';
import { IPromotionCreateObject } from '@ever-platform/common/src/interfaces/IPromotion';
import { first } from 'rxjs/operators';
import _ = require('lodash');

@injectable()
@routerName('promotion')
@@ -36,9 +37,32 @@ export class PromotionService extends DBService<Promotion> implements IService {
		}
	}

	async getAllPromotions(): Promise<any[]> {
		return this.find({
	async getAllPromotions(findInput: { warehouse: string }): Promise<any[]> {
		const warehousePromotions = await this.Model.find({
			warehouse: { $eq: findInput.warehouse },
			isDeleted: { $eq: false },
		})
			.select({
				title: 1,
				description: 1,
				active: 1,
				promoPrice: 1,
				activeFrom: 1,
				activeTo: 1,
				image: 1,
				product: 1,
				warehouse: 1,
				purchasesCount: 1,
			})
			.lean()
			.exec();

		return _.map(warehousePromotions, (p) => {
			return {
				...p,
				warehouseId: p.warehouse,
				productId: p.product,
			};
		});
	}

@@ -46,7 +70,7 @@ export class PromotionService extends DBService<Promotion> implements IService {
		const promotion = await this.get(promotionId).pipe(first()).toPromise();

		if (!promotion || promotion.isDeleted) {
			throw Error(`Prmotion with id '${promotionId}' does not exist!`);
			throw Error(`Promotion with id '${promotionId}' does not exist!`);
		}
	}
}
+1 −1
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ export class BasicInfoFormComponent implements OnInit, OnDestroy {
			),
			activeFrom: this.promotion.activeFrom || new Date(),
			activeTo: this.promotion.activeTo || null,
			product: this.promotion.product || null,
			product: this.promotion.productId || null,
		};

		this.form.patchValue(promotionFormValue);
Loading