Skip to content

Commit

Permalink
add tag for order
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Jul 14, 2024
1 parent 50fa740 commit 90bed72
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
32 changes: 32 additions & 0 deletions migrations/20240714154524-add-order-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
// sql template autogenerated by gen-db-migrates

var async = require('async')
var dbm;
var type;
var seed;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db, callback) {
async.series([
(cb) => db.addColumn('order', 'tag', {"type":"text"}, cb),

], callback);
}

exports.down = function(db) {
return null;
};

exports._meta = {
"version": 1
};
15 changes: 15 additions & 0 deletions models/Order.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ declare let attributes: {
*/
discountTotal: number;
orderDate: string;
/**
* @experimental
* This field allows you to somehow mark the recycle bin, although this is not used in current versions of the kernel.
* Designed for creating some custom logic, through visual programming or through modules.
*/
tag: string;
deviceId: string;
/**
* A number that will change every time the order is changed
Expand Down Expand Up @@ -220,6 +226,14 @@ declare let Model: {
order(criteria: CriteriaQuery<Order>): Promise<void>;
payment(criteria: CriteriaQuery<Order>): Promise<PaymentResponse>;
clear(criteria: CriteriaQuery<Order>): Promise<void>;
/**
* Method for quickly setting a Order tag
* @experimental
* @param criteria
* @param tag
* @returns
*/
tag(criteria: CriteriaQuery<Order>, tag: string): Promise<Order>;
setCustomData(criteria: CriteriaQuery<Order>, customData: any): Promise<void>;
paymentMethodId(criteria: CriteriaQuery<Order>): Promise<string>;
/** given populated Order instance by criteria*/
Expand Down Expand Up @@ -280,6 +294,7 @@ declare let Model: {
orderTotal?: number;
discountTotal?: number;
orderDate?: string;
tag?: string;
deviceId?: string;
nonce?: number;
hash?: string;
Expand Down
26 changes: 26 additions & 0 deletions models/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ let attributes = {
},
orderDate: "string",
// orderDateLimit: "string",
/**
* @experimental
* This field allows you to somehow mark the recycle bin, although this is not used in current versions of the kernel.
* Designed for creating some custom logic, through visual programming or through modules.
*/
tag: "string",
deviceId: "string",
/**
* A number that will change every time the order is changed
Expand Down Expand Up @@ -960,6 +966,26 @@ let Model = {
await Order.countCart({ id: order.id });
await emitter.emit.apply(emitter, ["core:order-was-cleared", ...arguments]);
},
/**
* Method for quickly setting a Order tag
* @experimental
* @param criteria
* @param tag
* @returns
*/
async tag(criteria, tag) {
emitter.emit.apply(emitter, ["core:order-set-tag", ...arguments]);
try {
let order = await Order.findOne(criteria);
if (order.state !== "CART") {
throw new Error(`Clear allowed only for CART state`);
}
return await Order.updateOne({ id: order.id }, { tag: tag }).fetch();
}
catch (error) {
sails.log.error('Error tagging order:', error);
}
},
async setCustomData(criteria, customData) {
await emitter.emit.apply(emitter, ["core:order-set-custom-data", ...arguments]);
let order = await Order.findOne(criteria);
Expand Down
29 changes: 29 additions & 0 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ let attributes = {
orderDate: "string",
// orderDateLimit: "string",

/**
* @experimental
* This field allows you to somehow mark the recycle bin, although this is not used in current versions of the kernel.
* Designed for creating some custom logic, through visual programming or through modules.
*/
tag: "string",

deviceId: "string",

/**
Expand Down Expand Up @@ -1178,6 +1185,28 @@ let Model = {
await emitter.emit.apply(emitter, ["core:order-was-cleared", ...arguments]);
},

/**
* Method for quickly setting a Order tag
* @experimental
* @param criteria
* @param tag
* @returns
*/
async tag(criteria: CriteriaQuery<Order>, tag: string): Promise<Order> {
emitter.emit.apply(emitter, ["core:order-set-tag", ...arguments]);
try {
let order = await Order.findOne(criteria);

if (order.state !== "CART") {
throw new Error(`Clear allowed only for CART state`);
}

return await Order.updateOne({ id: order.id }, { tag: tag }).fetch();
} catch (error) {
sails.log.error('Error tagging order:', error);
}
},

async setCustomData(criteria: CriteriaQuery<Order>, customData: any): Promise<void> {
await emitter.emit.apply(emitter, ["core:order-set-custom-data", ...arguments]);
let order = await Order.findOne(criteria);
Expand Down

0 comments on commit 90bed72

Please sign in to comment.