Skip to content

Commit

Permalink
new job to add users to default org
Browse files Browse the repository at this point in the history
  • Loading branch information
sandramchung committed Oct 8, 2018
1 parent d78fa49 commit 77c1cd4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/REFERENCE-environment_variables.md
Expand Up @@ -76,3 +76,5 @@ WAREHOUSE_DB_{TYPE,HOST,PORT,NAME,USER,PASSWORD} | Enables ability to load con
WAREHOUSE_DB_LAMBDA_ITERATION | If the WAREHOUSE_DB_ connection/feature is enabled, then on AWS Lambda, queries that take longer than 5min can expire. This will enable incrementing through queries on new lambda invocations to avoid timeouts.
WEBPACK_HOST | Host domain or IP for Webpack development server. _Default_: 127.0.0.1.
WEBPACK_PORT | Port for Webpack development server. _Defaut_: 3000.
FIX_ORGLESS | Set to any truthy value only if you want to run the job that automatically assigns the default org (see DEFAULT_ORG) to new users who have no assigned org.
DEFAULT_ORG | Set only with FIX_ORGLESS. Set to integer organization.id corresponding to the organization you want orgless users to be assigned to.
5 changes: 4 additions & 1 deletion src/workers/job-processes.js
Expand Up @@ -8,6 +8,7 @@ import { exportCampaign,
assignTexters,
sendMessages,
handleIncomingMessageParts,
fixOrgless,
clearOldJobs } from './jobs'
import { runMigrations } from '../migrations'
import { setupUserNotificationObservers } from '../server/notifications'
Expand Down Expand Up @@ -187,7 +188,8 @@ const processMap = {
messageSender234,
messageSender56,
messageSender789,
handleIncomingMessages
handleIncomingMessages,
fixOrgless
}

// if process.env.JOBS_SAME_PROCESS then we don't need to run
Expand All @@ -196,6 +198,7 @@ const syncProcessMap = {
// 'failedMessageSender': failedMessageSender, //see method for danger
handleIncomingMessages,
checkMessageQueue,
fixOrgless,
clearOldJobs
}

Expand Down
24 changes: 23 additions & 1 deletion src/workers/jobs.js
@@ -1,5 +1,6 @@
import { r, datawarehouse, cacheableData,
Assignment, Campaign, CampaignContact, Organization, User } from '../server/models'
Assignment, Campaign, CampaignContact, Organization, User,
UserOrganization } from '../server/models'
import { log, gunzip, zipToTimeZone, convertOffsetsToStrings } from '../lib'
import { updateJob } from './lib'
import { getFormattedPhoneNumber } from '../lib/phone-format.js'
Expand Down Expand Up @@ -917,6 +918,27 @@ export async function handleIncomingMessageParts() {
}
}

export async function fixOrgless() {
if (process.env.FIX_ORGLESS) {
const orgless = await r.knex
.select('user.id')
.from('user')
.leftJoin('user_organization', 'user.id', 'user_organization.user_id')
.whereNull('user_organization.id');
orgless.forEach(async(orglessUser) => {
await UserOrganization.save({
user_id: orglessUser.id.toString(),
organization_id: process.env.DEFAULT_ORG || 1,
role: 'TEXTER'
}).error(function(error) {
// Unexpected errors
console.log("error on userOrganization save in orgless", error)
});
console.log("added orgless user " + user.id + " to organization " + process.env.DEFAULT_ORG )
}) // forEach
} // if
} // function

export async function clearOldJobs(delay) {
// to clear out old stuck jobs
const twoHoursAgo = new Date(new Date() - 1000 * 60 * 60 * 2)
Expand Down

0 comments on commit 77c1cd4

Please sign in to comment.