Skip to content

Commit

Permalink
feat: add sale status on pos orders (#5038)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhBaterdene committed Mar 11, 2024
1 parent 9f894c1 commit 561cda0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
3 changes: 1 addition & 2 deletions packages/plugin-posclient-api/src/graphql/resolvers/index.ts
Expand Up @@ -9,15 +9,14 @@ import Cover from './cover';

const resolvers: any = async () => ({
...customScalars,

PosConfig,
PoscProduct,
Order,
OrderDetail: Order,
Cover,
PosOrderItem: OrderItem,
Mutation,
Query
Query,
});

export default resolvers;
Expand Up @@ -13,6 +13,7 @@ import {
BILL_TYPES,
ORDER_ITEM_STATUSES,
ORDER_STATUSES,
ORDER_SALE_STATUS,
ORDER_TYPES,
} from '../../../models/definitions/constants';
import { IPaidAmount } from '../../../models/definitions/orders';
Expand Down Expand Up @@ -124,6 +125,16 @@ export const getStatus = (config, buttonType, doc, order?) => {
return ORDER_STATUSES.NEW;
};

export const getSaleStatus = (config, doc, order) => {
if (order.saleStatus) {
if (order.saleStatus === ORDER_SALE_STATUS.CONFIRMED) {
return ORDER_SALE_STATUS.CONFIRMED;
}
return ORDER_SALE_STATUS.CART;
}
return ORDER_SALE_STATUS.CART;
};

const orderAdd = async (models: IModels, lastDoc, config) => {
try {
const number = await generateOrderNumber(models, config);
Expand Down Expand Up @@ -191,6 +202,7 @@ const ordersAdd = async (
);

const status = getStatus(config, doc.buttonType, doc);
const saleStatus = getSaleStatus(config, doc, preparedDoc);

const lastDoc = {
...doc,
Expand All @@ -202,6 +214,7 @@ const ordersAdd = async (
departmentId: config.departmentId,
taxInfo: getTaxInfo(config),
status,
saleStatus,
};

const order = await orderAdd(models, lastDoc, config);
Expand Down Expand Up @@ -304,6 +317,7 @@ const ordersEdit = async (
await updateOrderItems(doc._id, preparedDoc.items, models);

let status = getStatus(config, doc.buttonType, doc, order);
let saleStatus = getSaleStatus(config, doc, preparedDoc);

// dont change isPre
const updatedOrder = await models.Orders.updateOrder(doc._id, {
Expand All @@ -325,6 +339,7 @@ const ordersEdit = async (
dueDate: doc.dueDate,
description: doc.description,
status,
saleStatus,
});

await graphqlPubsub.publish('ordersOrdered', {
Expand Down Expand Up @@ -427,6 +442,22 @@ const orderMutations = {
return await models.Orders.getOrder(_id);
},

async orderChangeSaleStatus(
_root,
{ _id, saleStatus }: { _id: string; saleStatus: string },
{ models, subdomain, config }: IContext,
) {
const oldOrder = await models.Orders.getOrder(_id);

await models.Orders.updateOrder(_id, {
...oldOrder,
saleStatus,
modifiedAt: new Date(),
});

return await models.Orders.getOrder(_id);
},

async ordersChange(
_root,
params: IOrderChangeParams,
Expand Down Expand Up @@ -646,6 +677,7 @@ const orderMutations = {
? (order.cashAmount || 0) + Number(cashAmount.toFixed(2))
: order.cashAmount || 0,
paidAmounts: (order.paidAmounts || []).concat(paidAmounts || []),
saleStatus: ORDER_SALE_STATUS.CONFIRMED,
},
};

Expand Down
Expand Up @@ -17,6 +17,7 @@ interface ISearchParams {
customerType?: string;
isPaid?: boolean;
statuses: string[];
saleStatus: string;
dueStartDate?: Date;
dueEndDate?: Date;
isPreExclude?: boolean;
Expand All @@ -27,6 +28,7 @@ const generateFilter = (config: IConfig, params: ISearchParams) => {
const {
searchValue,
statuses,
saleStatus,
customerId,
startDate,
endDate,
Expand All @@ -38,6 +40,7 @@ const generateFilter = (config: IConfig, params: ISearchParams) => {
isPreExclude,
slotCode,
} = params;

const filter: any = {
$or: [{ posToken: config.token }, { subToken: config.token }],
};
Expand All @@ -57,6 +60,10 @@ const generateFilter = (config: IConfig, params: ISearchParams) => {
filter.slotCode = slotCode;
}

if (saleStatus) {
filter.saleStatus = saleStatus;
}

if (customerType) {
filter.customerType =
customerType === 'customer'
Expand All @@ -76,9 +83,11 @@ const generateFilter = (config: IConfig, params: ISearchParams) => {
if (startDate) {
dateQry.$gte = getPureDate(startDate);
}

if (endDate) {
dateQry.$lte = getPureDate(endDate);
}

if (Object.keys(dateQry).length) {
const dateTypes = {
paid: 'paidDate',
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-posclient-api/src/graphql/schema/orders.ts
Expand Up @@ -21,6 +21,7 @@ const paymentInputDefs = `
export const orderTypeFields = `
${commonFields}
status: String
saleStatus: String
customerId: String
number: String
${paymentInputDefs}
Expand Down Expand Up @@ -66,8 +67,9 @@ const addEditParams = `
origin: String,
dueDate: Date,
status: String,
buttonType: String
description: String
saleStatus: String,
buttonType: String,
description: String,
isPre: Boolean
`;

Expand Down Expand Up @@ -164,6 +166,7 @@ export const types = `
export const ordersQueryParams = `
searchValue: String,
statuses: [String],
saleStatus: String,
customerId: String,
customerType: String,
startDate: Date,
Expand All @@ -185,6 +188,7 @@ export const mutations = `
ordersEdit(_id: String!, ${addEditParams}): Order
ordersMakePayment(_id: String!, doc: OrderPaymentInput): PosPutResponse
orderChangeStatus(_id: String!, status: String): Order
orderChangeSaleStatus(_id: String!, saleStatus: String): Order
ordersChange(_id: String!, dueDate: Date, branchId: String, deliveryInfo: JSON, description: String): Order
ordersAddPayment(_id: String!, cashAmount: Float, mobileAmount: Float, paidAmounts: [PaidAmountInput] ): Order
ordersCancel(_id: String!): JSON
Expand Down
Expand Up @@ -58,6 +58,12 @@ export const ORDER_STATUSES = {
FULL: ['paid', 'done', 'complete'],
};

export const ORDER_SALE_STATUS = {
CART: 'cart',
CONFIRMED: 'confirmed',
ALL: ['cart', 'confirmed'],
};

export const ORDER_ITEM_STATUSES = {
NEW: 'new',
CONFIRM: 'confirm',
Expand Down
15 changes: 14 additions & 1 deletion packages/plugin-posclient-api/src/models/definitions/orders.ts
Expand Up @@ -6,7 +6,12 @@ import {
schemaHooksWrapper,
} from './utils';
import { IOrderItemDocument } from './orderItems';
import { BILL_TYPES, ORDER_STATUSES, ORDER_TYPES } from './constants';
import {
BILL_TYPES,
ORDER_STATUSES,
ORDER_TYPES,
ORDER_SALE_STATUS,
} from './constants';

export interface IPaidAmount {
_id?: string;
Expand All @@ -22,6 +27,7 @@ export interface IMobileAmount {

export interface IOrder {
status?: string;
saleStatus?: string;
createdAt?: Date;
modifiedAt?: Date;
userId?: string;
Expand Down Expand Up @@ -104,6 +110,13 @@ export const orderSchema = schemaHooksWrapper(
default: ORDER_STATUSES.NEW,
index: true,
}),
saleStatus: field({
type: String,
label: 'Status of the sale',
enum: ORDER_SALE_STATUS.ALL,
default: ORDER_SALE_STATUS.CART,
index: true,
}),
paidDate: field({ type: Date, label: 'Paid date' }),
dueDate: field({ type: Date, label: 'Due date' }),
number: field({
Expand Down

0 comments on commit 561cda0

Please sign in to comment.