68 lines
3.4 KiB
TypeScript
68 lines
3.4 KiB
TypeScript
import { Arg, Args, Ctx, Query, Resolver } from 'type-graphql';
|
|
|
|
import OrderModel from '@services/module-payments/functions/orders/order.model';
|
|
import { ApolloContext } from '@lib/seed/interfaces/context';
|
|
import { AccountTypeEnum } from '@src/accounts/account.components';
|
|
import { ProviderSchema } from '../../schemas/provider.schema';
|
|
import _ from 'lodash';
|
|
import { OrderBusinessArgs } from '../../schemas/order.schema.input';
|
|
import { EngineMiddleware } from '@lib/seed/graphql/MiddlewareV2';
|
|
import { OrderStatusEnum } from '@services/module-payments/components/components.orders';
|
|
import { PaymentStatusEnum } from '@services/module-payments/components/components.payments';
|
|
|
|
@Resolver(OrderModel)
|
|
export default class OrderBusinessEngineQueryResolver {
|
|
/*
|
|
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
|
|
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
|
|
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
|
|
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
|
|
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
|
|
*/
|
|
|
|
@Query(() => ProviderSchema)
|
|
@EngineMiddleware({
|
|
authorization: [AccountTypeEnum.organisationOwner, AccountTypeEnum.storeManager],
|
|
checkOrganisation: true,
|
|
})
|
|
async businessOrdersGetOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<ProviderSchema | undefined> {
|
|
const organisationId = ctx.ctx.organisationId;
|
|
const order = await new OrderModel().getOne({ query: { 'providerOrderItems.organisationId': organisationId, 'providerOrderItems._id': id } });
|
|
|
|
return _.find(order.providerOrderItems, { _id: id });
|
|
}
|
|
|
|
@Query(() => [ProviderSchema], { nullable: 'itemsAndList' })
|
|
@EngineMiddleware({
|
|
authorization: [AccountTypeEnum.organisationOwner, AccountTypeEnum.storeManager],
|
|
checkOrganisation: true,
|
|
})
|
|
async businessOrdersGetMany(@Args() args: OrderBusinessArgs, @Ctx() ctx: ApolloContext): Promise<ProviderSchema[]> {
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
const organisationId = ctx.ctx.organisationId!;
|
|
const { pagination, ...arg } = args;
|
|
|
|
// Get only those where orderStatus and paymentStatus is correct
|
|
|
|
const filters: any = {
|
|
'providerOrderItems.organisationId': organisationId,
|
|
orderStatus: { $in: [OrderStatusEnum.processing, OrderStatusEnum.shipped, OrderStatusEnum.completed] },
|
|
paymentStatus: { $in: [PaymentStatusEnum.paid] },
|
|
};
|
|
if (arg.orderStatus) filters['providerOrderItems.orderStatus'] = { $in: arg.orderStatus };
|
|
|
|
const orders = await new OrderModel().getMany({ query: filters, pagination });
|
|
const results: ProviderSchema[] = [];
|
|
|
|
orders.forEach((order) => {
|
|
if (order.providerOrderItems) {
|
|
const index = _.findIndex(order.providerOrderItems, { organisationId: organisationId });
|
|
if (index !== -1) results.push(order.providerOrderItems[index]);
|
|
}
|
|
});
|
|
|
|
return results;
|
|
}
|
|
}
|