@rsc-labs/medusa-store-analytics
Version:
Get analytics data about your store
115 lines (114 loc) • 5.04 kB
JavaScript
;
/*
* Copyright 2024 RSC-Labs, https://rsoftcon.com/
*
* MIT License
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const medusa_1 = require("@medusajs/medusa");
const medusa_2 = require("@medusajs/medusa");
const typeorm_1 = require("typeorm");
const dateTransformations_1 = require("./utils/dateTransformations");
class MarketingAnalyticsService extends medusa_1.TransactionBaseService {
constructor(container) {
super(container);
this.TOP_LIMIT = 5;
}
async getTopDiscountsByCount(orderStatuses, from, to) {
const orderStatusesAsStrings = Object.values(orderStatuses);
if (orderStatusesAsStrings.length) {
if (from && to) {
const query = this.activeManager_
.getRepository(medusa_2.Order)
.createQueryBuilder('order')
.innerJoin("order.discounts", "discount")
.select("discount.code", "code")
.addSelect("discount.id", "discount_id")
.addSelect("COUNT(discount.id)", "sum")
.where('order.created_at >= :from', { from })
.where('order.created_at <= :to', { to })
.andWhere(`order.status IN(:...orderStatusesAsStrings)`, { orderStatusesAsStrings });
const topDiscounts = await query
.groupBy('discount.code, discount.id')
.orderBy('sum', 'DESC')
.setParameters({ from, to })
.limit(this.TOP_LIMIT)
.getRawMany();
return {
dateRangeFrom: from.getTime(),
dateRangeTo: to.getTime(),
dateRangeFromCompareTo: undefined,
dateRangeToCompareTo: undefined,
current: topDiscounts.map(result => ({
sum: result.sum,
discountCode: result.code,
discountId: result.discount_id
})),
previous: undefined
};
}
let startQueryFrom;
if (from) {
startQueryFrom = from;
}
else {
// All time
const lastOrder = await this.activeManager_.getRepository(medusa_2.Order).find({
skip: 0,
take: 1,
order: { created_at: "ASC" },
where: { status: (0, typeorm_1.In)(orderStatusesAsStrings) }
});
if (lastOrder.length > 0) {
startQueryFrom = lastOrder[0].created_at;
}
}
if (startQueryFrom) {
const endQuery = (0, dateTransformations_1.getQueryEndDate)(to);
const query = this.activeManager_
.getRepository(medusa_2.Order)
.createQueryBuilder('order')
.innerJoin("order.discounts", "discount")
.select("discount.code", "code")
.addSelect("discount.id", "discount_id")
.addSelect("COUNT(discount.id)", "sum")
.where('order.created_at >= :startQueryFrom', { startQueryFrom })
.where('order.created_at <= :endQuery', { endQuery })
.andWhere(`order.status IN(:...orderStatusesAsStrings)`, { orderStatusesAsStrings });
const topDiscounts = await query
.groupBy('discount.code, discount.id')
.orderBy('sum', 'DESC')
.setParameters({ startQueryFrom, endQuery })
.limit(this.TOP_LIMIT)
.getRawMany();
return {
dateRangeFrom: startQueryFrom.getTime(),
dateRangeTo: to ? to.getTime() : new Date(Date.now()).getTime(),
dateRangeFromCompareTo: undefined,
dateRangeToCompareTo: undefined,
current: topDiscounts.map(result => ({
sum: result.sum,
discountCode: result.code,
discountId: result.discount_id
})),
previous: undefined
};
}
}
return {
dateRangeFrom: undefined,
dateRangeTo: undefined,
dateRangeFromCompareTo: undefined,
dateRangeToCompareTo: undefined,
current: [],
previous: undefined
};
}
}
exports.default = MarketingAnalyticsService;