|
| 1 | +import * as crud from "back-end/lib/crud"; |
| 2 | +import * as db from "back-end/lib/db"; |
| 3 | +import * as permissions from "back-end/lib/permissions"; |
| 4 | +import { |
| 5 | + basicResponse, |
| 6 | + FileResponseBody, |
| 7 | + JsonResponseBody, |
| 8 | + makeJsonResponseBody, |
| 9 | + nullRequestBodyHandler |
| 10 | +} from "back-end/lib/server"; |
| 11 | +import { Session } from "shared/lib/resources/session"; |
| 12 | +import { UserType, userTypeToTitleCase } from "shared/lib/resources/user"; |
| 13 | +import { adt } from "shared/lib/types"; |
| 14 | +import { isValid } from "shared/lib/validation"; |
| 15 | +import { getString } from "shared/lib"; |
| 16 | +import { |
| 17 | + validateContactListExportParams, |
| 18 | + ExportContactListValidationErrors |
| 19 | +} from "back-end/lib/validation"; |
| 20 | + |
| 21 | +const routeNamespace = "contact-list"; |
| 22 | + |
| 23 | +const readMany: crud.ReadMany<Session, db.Connection> = ( |
| 24 | + connection: db.Connection |
| 25 | +) => { |
| 26 | + return nullRequestBodyHandler< |
| 27 | + FileResponseBody | JsonResponseBody<ExportContactListValidationErrors>, |
| 28 | + Session |
| 29 | + >(async (request) => { |
| 30 | + const respond = (code: number, body: ExportContactListValidationErrors) => |
| 31 | + basicResponse(code, request.session, makeJsonResponseBody(body)); |
| 32 | + |
| 33 | + // Check admin permissions |
| 34 | + if (!permissions.isAdmin(request.session)) { |
| 35 | + return respond(401, { |
| 36 | + permissions: [permissions.ERROR_MESSAGE] |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + // Parse query parameters |
| 41 | + const userTypesParam = getString(request.query, "userTypes"); |
| 42 | + const fieldsParam = getString(request.query, "fields"); |
| 43 | + |
| 44 | + const userTypes = userTypesParam |
| 45 | + ? userTypesParam.split(",").filter((type) => type.trim() !== "") |
| 46 | + : []; |
| 47 | + const fields = fieldsParam |
| 48 | + ? fieldsParam.split(",").filter((field) => field.trim() !== "") |
| 49 | + : []; |
| 50 | + |
| 51 | + // Validate input parameters using the new validation functions |
| 52 | + const validationResult = validateContactListExportParams(userTypes, fields); |
| 53 | + |
| 54 | + if (!isValid(validationResult)) { |
| 55 | + return respond(400, validationResult.value); |
| 56 | + } |
| 57 | + |
| 58 | + const { userTypes: validatedUserTypes, fields: validatedFields } = |
| 59 | + validationResult.value; |
| 60 | + |
| 61 | + try { |
| 62 | + const userTypesToFetch: UserType[] = []; |
| 63 | + if (validatedUserTypes.includes(UserType.Government)) { |
| 64 | + userTypesToFetch.push(UserType.Government, UserType.Admin); |
| 65 | + } |
| 66 | + if (validatedUserTypes.includes(UserType.Vendor)) { |
| 67 | + userTypesToFetch.push(UserType.Vendor); |
| 68 | + } |
| 69 | + |
| 70 | + const usersWithOrganizations = await db.readManyUsersWithOrganizations( |
| 71 | + connection, |
| 72 | + userTypesToFetch, |
| 73 | + false |
| 74 | + ); |
| 75 | + |
| 76 | + const headerRow: string[] = []; |
| 77 | + |
| 78 | + if (validatedFields.includes("firstName")) headerRow.push("First Name"); |
| 79 | + if (validatedFields.includes("lastName")) headerRow.push("Last Name"); |
| 80 | + if (validatedFields.includes("email")) headerRow.push("Email"); |
| 81 | + |
| 82 | + // Add User Type column if both Government and Vendor types are selected |
| 83 | + const includeUserType = |
| 84 | + validatedUserTypes.includes(UserType.Government) && |
| 85 | + validatedUserTypes.includes(UserType.Vendor); |
| 86 | + if (includeUserType) headerRow.push("User Type"); |
| 87 | + |
| 88 | + if (validatedFields.includes("organizationName")) |
| 89 | + headerRow.push("Organization Name"); |
| 90 | + |
| 91 | + let csvContent = headerRow.join(",") + "\n"; |
| 92 | + |
| 93 | + if (isValid(usersWithOrganizations)) { |
| 94 | + for (const userWithOrgs of usersWithOrganizations.value) { |
| 95 | + const user = userWithOrgs.user; |
| 96 | + const row: string[] = []; |
| 97 | + |
| 98 | + const nameParts = (user.name || "").split(" "); |
| 99 | + const firstName = nameParts[0] || ""; |
| 100 | + const lastName = |
| 101 | + nameParts.length > 1 ? nameParts.slice(1).join(" ") : ""; |
| 102 | + |
| 103 | + if (validatedFields.includes("firstName")) row.push(`"${firstName}"`); |
| 104 | + if (validatedFields.includes("lastName")) row.push(`"${lastName}"`); |
| 105 | + if (validatedFields.includes("email")) |
| 106 | + row.push(`"${user.email || ""}"`); |
| 107 | + |
| 108 | + // Add user type if both Government and Vendor types are selected |
| 109 | + if (includeUserType) { |
| 110 | + const userTypeLabel = |
| 111 | + user.type === UserType.Admin |
| 112 | + ? "Admin" |
| 113 | + : userTypeToTitleCase(user.type); |
| 114 | + row.push(`"${userTypeLabel}"`); |
| 115 | + } |
| 116 | + |
| 117 | + if (validatedFields.includes("organizationName")) { |
| 118 | + const orgNamesString = |
| 119 | + userWithOrgs.organizationNames.length > 0 |
| 120 | + ? userWithOrgs.organizationNames.join("; ") |
| 121 | + : ""; |
| 122 | + row.push(`"${orgNamesString}"`); |
| 123 | + } |
| 124 | + |
| 125 | + csvContent += row.join(",") + "\n"; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return basicResponse( |
| 130 | + 200, |
| 131 | + request.session, |
| 132 | + adt("file", { |
| 133 | + buffer: Buffer.from(csvContent, "utf-8"), |
| 134 | + contentType: "text/csv", |
| 135 | + contentDisposition: "attachment; filename=dm-contacts.csv" |
| 136 | + }) |
| 137 | + ); |
| 138 | + } catch (error) { |
| 139 | + request.logger.error( |
| 140 | + "Error generating contact list CSV", |
| 141 | + error as object |
| 142 | + ); |
| 143 | + return respond(500, { |
| 144 | + permissions: ["An error occurred while generating the contact list"] |
| 145 | + }); |
| 146 | + } |
| 147 | + }); |
| 148 | +}; |
| 149 | + |
| 150 | +const resource: crud.BasicCrudResource<Session, db.Connection> = { |
| 151 | + routeNamespace, |
| 152 | + readMany |
| 153 | +}; |
| 154 | + |
| 155 | +export default resource; |
0 commit comments