|
1 | 1 | const db = require('../lib/db') |
2 | 2 | const team = require('./team') |
3 | | -const { map, prop, includes, has, isNil, propEq, find } = require('ramda') |
| 3 | +const { map, prop, includes, has, isNil } = require('ramda') |
4 | 4 | const { unpack, PropertyRequiredError } = require('../../app/lib/utils') |
5 | 5 | const { serverRuntimeConfig } = require('../../next.config') |
6 | 6 | const { DEFAULT_PAGE_SIZE } = serverRuntimeConfig |
@@ -91,6 +91,9 @@ async function create(data, osmId) { |
91 | 91 |
|
92 | 92 | if (!data.name) throw new Error('data.name property is required') |
93 | 93 |
|
| 94 | + // Cache username |
| 95 | + await team.resolveMemberNames([osmId]) |
| 96 | + |
94 | 97 | return db.transaction(async (trx) => { |
95 | 98 | const [row] = await trx('organization') |
96 | 99 | .insert(data) |
@@ -186,6 +189,9 @@ async function removeOwner(id, osmId) { |
186 | 189 | * @return {promise} |
187 | 190 | */ |
188 | 191 | async function addManager(id, osmId) { |
| 192 | + // Cache username |
| 193 | + await team.resolveMemberNames([osmId]) |
| 194 | + |
189 | 195 | const isAlreadyManager = await isManager(id, osmId) |
190 | 196 |
|
191 | 197 | // Only ids that are not already in manager list should be added. Duplicate requests should fail silently |
@@ -232,6 +238,9 @@ async function removeManager(id, osmId) { |
232 | 238 | * @return {promise} |
233 | 239 | */ |
234 | 240 | async function createOrgTeam(organizationId, data, osmId) { |
| 241 | + // Cache username |
| 242 | + await team.resolveMemberNames([osmId]) |
| 243 | + |
235 | 244 | return db.transaction(async (trx) => { |
236 | 245 | const record = await team.create(data, osmId, trx) |
237 | 246 | await trx('organization_team').insert({ |
@@ -438,44 +447,54 @@ async function getOrgStaff(options) { |
438 | 447 | async function getOrgStaffPaginated(organizationId, options = {}) { |
439 | 448 | // Get owners |
440 | 449 | let ownerQuery = db('organization_owner') |
441 | | - .select(db.raw("organization_id, osm_id, 'owner' as type")) |
442 | | - .where('organization_id', organizationId) |
| 450 | + .join('osm_users', 'organization_owner.osm_id', 'osm_users.id') |
| 451 | + .select( |
| 452 | + 'organization_owner.organization_id', |
| 453 | + 'organization_owner.osm_id', |
| 454 | + db.raw("'owner' as type"), |
| 455 | + 'osm_users.name' |
| 456 | + ) |
| 457 | + .where('organization_owner.organization_id', organizationId) |
| 458 | + |
| 459 | + // Apply search to owners sub-query |
| 460 | + if (options.search) { |
| 461 | + ownerQuery = ownerQuery.whereILike('osm_users.name', `%${options.search}%`) |
| 462 | + } |
443 | 463 |
|
444 | 464 | // Get managers that are not owners |
445 | 465 | let managerQuery = db('organization_manager') |
446 | | - .select(db.raw("organization_id, osm_id, 'manager' as type")) |
447 | | - .where('organization_id', organizationId) |
| 466 | + .join('osm_users', 'organization_manager.osm_id', 'osm_users.id') |
| 467 | + .select( |
| 468 | + 'organization_manager.organization_id', |
| 469 | + 'organization_manager.osm_id', |
| 470 | + db.raw("'manager' as type"), |
| 471 | + 'osm_users.name' |
| 472 | + ) |
| 473 | + .where('organization_manager.organization_id', organizationId) |
448 | 474 | .whereNotIn( |
449 | | - 'osm_id', |
| 475 | + 'organization_manager.osm_id', |
450 | 476 | db('organization_owner') |
451 | | - .select('osm_id') |
| 477 | + .select('organization_owner.osm_id') |
452 | 478 | .where('organization_id', organizationId) |
453 | 479 | ) |
454 | 480 |
|
455 | | - // Execute query with pagination |
456 | | - const result = await ownerQuery.unionAll(managerQuery).paginate({ |
| 481 | + // Apply search managers sub-query |
| 482 | + if (options.search) { |
| 483 | + managerQuery = managerQuery.whereILike( |
| 484 | + 'osm_users.name', |
| 485 | + `%${options.search}%` |
| 486 | + ) |
| 487 | + } |
| 488 | + |
| 489 | + // Unite owner and manager queries |
| 490 | + let staffQuery = ownerQuery.unionAll(managerQuery) |
| 491 | + |
| 492 | + // Execute staff query with pagination |
| 493 | + return await staffQuery.paginate({ |
457 | 494 | isLengthAware: true, |
458 | 495 | currentPage: options.page || 1, |
459 | 496 | perPage: options.perPage || DEFAULT_PAGE_SIZE, |
460 | 497 | }) |
461 | | - |
462 | | - // Get osm user meta from ids |
463 | | - const osmUsers = await team.resolveMemberNames( |
464 | | - result.data.map((m) => m.osm_id) |
465 | | - ) |
466 | | - |
467 | | - return { |
468 | | - ...result, |
469 | | - // Apply OSM display names to results |
470 | | - data: result.data.map((u) => { |
471 | | - const osmUser = find(propEq('id', u.osm_id))(osmUsers) |
472 | | - return { |
473 | | - ...u, |
474 | | - id: u.osm_id, |
475 | | - name: osmUser?.name || '', |
476 | | - } |
477 | | - }), |
478 | | - } |
479 | 498 | } |
480 | 499 |
|
481 | 500 | /** |
|
0 commit comments