Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lchanmann/7919fd035c53a7e5a3a07c1ddf213067 to your computer and use it in GitHub Desktop.
Save lchanmann/7919fd035c53a7e5a3a07c1ddf213067 to your computer and use it in GitHub Desktop.
import foam.mlang.MLang;
import foam.nanos.auth.LifecycleState;
import foam.nanos.auth.User;
import foam.nanos.crunch.UserCapabilityJunction;
import foam.nanos.crunch.CapabilityJunctionStatus;
import foam.nanos.logger.PrefixLogger;
import net.nanopay.admin.model.ComplianceStatus;
/*
* Run configs
*/
DRY_RUN = true; // Set to true to run simulation (default) or false to run and update data.
N = 5000; // Set the number of ucjs to update. N must be non-zero positive integer.
SKIP_BUS_IDS // Set business ids to skip the ucj update.
= new Long[] {
// 50203
};
/*
* DO NOT MODIFY BELOW CODE
*/
ONBOARDING_CAP_ID = "crunch.onboarding.api.ca-business-receive-payments";
TRANSACTION_CAP_ID = "crunch.onboarding.transaction.ca-business-receive-payments";
MERCHANT_AGENT_CAP_ID = "crunch.onboarding.business-user";
// Variables
scriptName = currentScript != null ? currentScript.id : "Script";
logger = new PrefixLogger(new Object[] { scriptName }, x.get("logger"));
businessDAO = x.get("localBusinessDAO");
crunchService = x.get("crunchService");
i = 0;
// Helper functions
void addMerchantAgentACJ(business, user) {
info = i + ". " + business.id + "/" + user.id;
if ( ! DRY_RUN ) {
acj = crunchService.updateJunctionFor(x, MERCHANT_AGENT_CAP_ID, null, CapabilityJunctionStatus.GRANTED, business, user);
logger.debug(new Object[] { "updated junction", acj.id, "status:" + acj.status });
info = "✔ " + info + ", status: " + acj.status;
}
print(info);
}
// Main
merchants = businessDAO
.where(MLang.NEQ(User.EXTERNAL_ID, "PAYER"))
.where(MLang.EQ(User.LIFECYCLE_STATE, LifecycleState.ACTIVE))
.where(MLang.EQ(User.COMPLIANCE, ComplianceStatus.PASSED))
.where(MLang.EQ(User.SPID, "intuit"))
.where(MLang.NOT(MLang.IN(User.ID, SKIP_BUS_IDS)))
.select()
.array;
print("Updating merchants/agents <" + MERCHANT_AGENT_CAP_ID + "> acj:");
// Check merchant's onboarding ucj and transaction ucj, if they both NOT granted
// then add the "Associated Busienss User" acj for the merchant/agent pair.
//
// The "Associated Busienss User" acj status will be granted if all its prereq
// tree is granted, otherwise the status will be in action_required.
// After saving the acj, ReputDependentUCJs rule will be triggered and update
// the parent ucj (i.e, "crunch.onboarding.api.ca-business-receive-payments")
// status automatically.
for ( business : merchants ) {
try {
onboardingUcj = crunchService.getJunctionFor(x, ONBOARDING_CAP_ID, business, business);
if ( onboardingUcj == null || onboardingUcj.status != CapabilityJunctionStatus.GRANTED ) {
transactionUcj = crunchService.getJunctionFor(x, TRANSACTION_CAP_ID, business, business);
if ( transactionUcj == null || transactionUcj.status != CapabilityJunctionStatus.GRANTED ) {
if ( ++i >= N )
break;
// Get agent
users = business.getAgents(x).getDAO()
.where(MLang.EQ(User.LIFECYCLE_STATE, LifecycleState.ACTIVE))
.limit(1)
.select()
.array;
if ( users.size() != 1 ) {
logger.warning(new Object[] { "found merchant without an agent", "business id:" + business.id });
print(i + ". " + business.id + "/no agent");
continue;
}
// Add "Associated Busienss User" acj
addMerchantAgentACJ(business, users.get(0));
}
}
} catch ( Exception e ) {
print("× Failed business: "+ business.id + ". " + e.getMessage());
}
}
print("\n" + (DRY_RUN ? "DRY_RUN " : "UPDATED ") + i + "/" + merchants.size() + " merchants.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment