SyncMadeEasy/force-app/main/default/classes/FeatureParameter.cls

86 lines
4.1 KiB
OpenEdge ABL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

public with sharing class FeatureParameter {
/* -------------------- accumulator -------------------- */
private static Map<String,Integer> pending = new Map<String,Integer>();
private static Boolean jobQueued = false;
/* -------------------- existing future (unchanged) -------------------- */
@future
public static void incrementAsync(Map<String,Integer> deltas) {
if (deltas == null || deltas.isEmpty()) {
System.debug('FeatureParameter ➜ empty deltas, nothing to do.');
return;
}
for (String fn : deltas.keySet()) {
Integer oldVal = System.FeatureManagement.checkPackageIntegerValue(fn);
Integer newVal = oldVal + deltas.get(fn);
System.FeatureManagement.setPackageIntegerValue(fn, newVal);
System.debug(
'FeatureParameter ➜ fn=' + fn +
' old=' + oldVal +
' delta=' + deltas.get(fn) +
' new=' + newVal
);
}
}
/* -------------------- accumulate only (no future here) -------------------- */
private static void enqueueDelta(String feature, Integer qty){
Integer old = pending.get(feature);
pending.put(feature, (old == null ? 0 : old) + qty);
if (!jobQueued && Limits.getFutureCalls() < Limits.getLimitFutureCalls()){
jobQueued = true; // mark that we WILL flush later
}
}
/* -------------------- caller must invoke this ONCE after all DML -------------------- */
public static void flush(){
if (!pending.isEmpty()){
incrementAsync(pending); // single future with total deltas
}
pending.clear();
jobQueued = false;
}
/* -------------------- public helpers (only accumulate) -------------------- */
//public static void incrementAccountCount() { enqueueDelta('HIC_ShopifySync__Customer_Count', 1); }
public static void incrementOrderCount() { enqueueDelta('HIC_ShopifySync__Active_Order_Count', 1); }
public static void incrementOrderItemCount() { enqueueDelta('HIC_ShopifySync__Active_Order_Product_Count', 1); }
//public static void incrementProductCount() { enqueueDelta('HIC_ShopifySync__Product_Count', 1); }
//public static void incrementAccountCount(Integer n) { enqueueDelta('HIC_ShopifySync__Customer_Count', n); }
public static void incrementOrderCount(Integer n) { enqueueDelta('HIC_ShopifySync__Active_Order_Count', n); }
public static void incrementOrderItemCount(Integer n) { enqueueDelta('HIC_ShopifySync__Active_Order_Product_Count', n); }
//public static void incrementProductCount(Integer n) { enqueueDelta('HIC_ShopifySync__Product_Count', n); }
/* -------------------- absolute-value setters -------------------- */
public static void setActiveUserCount(Integer count) {
if (System.isFuture() || System.isBatch() || System.isQueueable()) {
// Already in async context – set directly
if (!Test.isRunningTest()) {
System.FeatureManagement.setPackageIntegerValue('HIC_ShopifySync__Active_User_Count', count);
}
} else {
try {
// @future is not allowed in post-install script context; this will throw in that case
setActiveUserCountAsync(count);
} catch (Exception e) {
// Fallback: set synchronously (e.g. during package install post-install script)
if (!Test.isRunningTest()) {
System.FeatureManagement.setPackageIntegerValue('HIC_ShopifySync__Active_User_Count', count);
}
System.debug('FeatureParameter ➜ @future unavailable, set Active_User_Count synchronously to ' + count);
}
}
}
@future
public static void setActiveUserCountAsync(Integer count) {
if (!Test.isRunningTest()) {
System.FeatureManagement.setPackageIntegerValue('HIC_ShopifySync__Active_User_Count', count);
}
System.debug('FeatureParameter ➜ Active_User_Count set to ' + count);
}
}