Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/src/services/listing-csv-export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ export const formatCommunityType = {
};

export const formatCloudinaryPdfUrl = (fileId: string): string => {
return `https://res.cloudinary.com/${process.env.CLOUDINARY_CLOUD_NAME}/image/upload/${fileId}.pdf`;
const cloudinaryCloudName: string | undefined =
process.env.CLOUDINARY_CLOUD_NAME || process.env.cloudinaryCloudName;
if (!cloudinaryCloudName) {
return fileId;
}
return `https://res.cloudinary.com/${cloudinaryCloudName}/image/upload/${fileId}.pdf`;
};

@Injectable()
Expand Down
35 changes: 34 additions & 1 deletion api/test/unit/services/listing-csv-export.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
import dayjs from 'dayjs';
import { randomUUID } from 'crypto';
import fs from 'fs';
import { ListingCsvExporterService } from '../../../src/services/listing-csv-export.service';
import {
formatCloudinaryPdfUrl,
ListingCsvExporterService,
} from '../../../src/services/listing-csv-export.service';
import { PrismaService } from '../../../src/services/prisma.service';
import Listing from '../../../src/dtos/listings/listing.dto';
import { User } from '../../../src/dtos/users/user.dto';
Expand Down Expand Up @@ -66,6 +69,7 @@ describe('Testing listing csv export service', () => {
visibleNeighborhoodAmenities: [],
regions: [],
visibleAccessibilityPriorityTypes: [],
visibleSpokenLanguages: [],
};

const mockBaseUnit: Unit = {
Expand Down Expand Up @@ -207,6 +211,35 @@ describe('Testing listing csv export service', () => {
},
};

describe('formatCloudinaryPdfUrl', () => {
const oldEnv = process.env;

beforeEach(() => {
process.env = { ...oldEnv };
});

afterAll(() => {
process.env = oldEnv;
});

it('should return original url when cloudinary env is not configured', () => {
delete process.env.CLOUDINARY_CLOUD_NAME;
delete process.env.cloudinaryCloudName;

expect(formatCloudinaryPdfUrl('https://aws.example.com/file.pdf')).toBe(
'https://aws.example.com/file.pdf',
);
});

it('should return cloudinary url when cloudinary env is configured', () => {
process.env.CLOUDINARY_CLOUD_NAME = 'exygy';

expect(formatCloudinaryPdfUrl('buildingSelectionCriteriaFileId')).toBe(
'https://res.cloudinary.com/exygy/image/upload/buildingSelectionCriteriaFileId.pdf',
);
});
});

describe('createCsv', () => {
it('should create the listing csv with no feature flags', async () => {
await service.createCsv('sampleFile.csv', undefined, {
Expand Down
31 changes: 31 additions & 0 deletions shared-helpers/__tests__/pdfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ import { ListingEvent, ListingEventsTypeEnum } from "../src/types/backend-swagge
afterEach(cleanup)

describe("pdfs helpers", () => {
const oldEnv = process.env

beforeEach(() => {
process.env = { ...oldEnv }
})

afterAll(() => {
process.env = oldEnv
})
it("should format cloudinary url", () => {
expect(cloudinaryPdfFromId("1234", "exygy")).toBe(
`https://res.cloudinary.com/exygy/image/upload/1234.pdf`
)
})

it("should return original id when cloudinary env is not set", () => {
delete process.env.CLOUDINARY_CLOUD_NAME
delete process.env.cloudinaryCloudName

expect(cloudinaryPdfFromId("https://aws.example.com/file.pdf")).toBe(
"https://aws.example.com/file.pdf"
)
})
it("should return correct pdf url for event if event type exists and file is cloudinary type", () => {
const listingEvents = [
{
Expand Down Expand Up @@ -39,4 +57,17 @@ describe("pdfs helpers", () => {
pdfUrlFromListingEvents(listingEvents, ListingEventsTypeEnum.publicLottery, "exygy")
).toBe(null)
})

it("should return original event file id if cloudinary env is not set", () => {
const listingEvents = [
{
type: ListingEventsTypeEnum.lotteryResults,
assets: { fileId: "https://aws.example.com/lottery-results.pdf", label: "cloudinaryPDF" },
},
] as ListingEvent[]

expect(
pdfUrlFromListingEvents(listingEvents, ListingEventsTypeEnum.lotteryResults, undefined)
).toBe("https://aws.example.com/lottery-results.pdf")
})
})
9 changes: 9 additions & 0 deletions shared-helpers/__tests__/photos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ describe("photos helper", () => {
)
})

it("should return original id when cloudinary env is not set", () => {
delete process.env.CLOUDINARY_CLOUD_NAME
delete process.env.cloudinaryCloudName

expect(cloudinaryUrlFromId("https://aws.example.com/image.jpg")).toBe(
"https://aws.example.com/image.jpg"
)
})

it("should return correct cloudinary url from a listing with new image field", () => {
process.env.CLOUDINARY_CLOUD_NAME = "exygy"

Expand Down
14 changes: 11 additions & 3 deletions shared-helpers/src/utilities/pdfs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { ListingEvent, ListingEventsTypeEnum } from "../types/backend-swagger"

export const cloudinaryPdfFromId = (publicId: string, cloudName: string) => {
return `https://res.cloudinary.com/${cloudName}/image/upload/${publicId}.pdf`
const configuredCloudName = (cloudName?: string) => {
return cloudName || process.env.cloudinaryCloudName || process.env.CLOUDINARY_CLOUD_NAME
}

export const cloudinaryPdfFromId = (publicId: string, cloudName?: string) => {
const resolvedCloudName = configuredCloudName(cloudName)
if (!resolvedCloudName) {
return publicId
}
return `https://res.cloudinary.com/${resolvedCloudName}/image/upload/${publicId}.pdf`
}

export const pdfUrlFromListingEvents = (
events: ListingEvent[],
listingEventType: ListingEventsTypeEnum,
cloudName: string
cloudName?: string
) => {
const event = events.find((event) => event?.type === listingEventType)
if (event) {
Expand Down
8 changes: 6 additions & 2 deletions shared-helpers/src/utilities/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ export const CLOUDINARY_BUILDING_LABEL = "cloudinaryBuilding"
export const IMAGE_FALLBACK_URL = "/images/listing-fallback.png"

export const cloudinaryUrlFromId = (publicId: string, size = 400) => {
const cloudName = process.env.cloudinaryCloudName || process.env.CLOUDINARY_CLOUD_NAME
return `https://res.cloudinary.com/${cloudName}/image/upload/w_${size},c_limit,q_65/${publicId}.jpg`
const cloudinaryCloudName: string | undefined =
process.env.cloudinaryCloudName || process.env.CLOUDINARY_CLOUD_NAME
if (!cloudinaryCloudName) {
return publicId
}
return `https://res.cloudinary.com/${cloudinaryCloudName}/image/upload/w_${size},c_limit,q_65/${publicId}.jpg`
}

export const getUrlForListingImage = (image: Asset, size = 400) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,12 @@ const BuildingSelectionCriteria = () => {
Pass the file for the dropzone callback along to the uploader
*/
const pdfUploader = async (file: File) => {
void (await cloudinaryFileUploader({ file, setCloudinaryData, setProgressValue }))
if (process.env.cloudinaryCloudName) {
void (await cloudinaryFileUploader({ file, setCloudinaryData, setProgressValue }))
} else {
// TODO: Upload to AWS
alert("Cloudinary environment variables not set, must configure AWS")
}
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,16 @@ const ListingPhotos = (props: ListingPhotosProps) => {
Pass the file for the dropzone callback along to the uploader
*/
const photoUploader = async (file: File) => {
void (await cloudinaryFileUploader({
file,
setCloudinaryData: setLatestUpload,
setProgressValue,
}))
if (process.env.cloudinaryCloudName) {
void (await cloudinaryFileUploader({
file,
setCloudinaryData: setLatestUpload,
setProgressValue,
}))
} else {
// TODO: Upload to AWS
alert("Cloudinary environment variables not set, must configure AWS")
}
}

const saveEditedPhoto = (newDescription: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ const LotteryResults = (props: LotteryResultsProps) => {
Pass the file for the dropzone callback along to the uploader
*/
const pdfUploader = async (file: File) => {
void (await cloudinaryFileUploader({ file, setCloudinaryData, setProgressValue }))
if (process.env.cloudinaryCloudName) {
void (await cloudinaryFileUploader({ file, setCloudinaryData, setProgressValue }))
} else {
// TODO: Upload to AWS
alert("Cloudinary environment variables not set, must configure AWS")
}
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,25 @@ const MarketingFlyer = ({ currentData, onSubmit }: MarketingFlyerProps) => {
].filter(Boolean)

const pdfUploader = async (file: File) => {
await cloudinaryFileUploader({ file, setCloudinaryData, setProgressValue })
if (process.env.cloudinaryCloudName) {
await cloudinaryFileUploader({ file, setCloudinaryData, setProgressValue })
} else {
// TODO: Upload to AWS
alert("Cloudinary environment variables not set, must configure AWS")
}
}

const accessiblePdfUploader = async (file: File) => {
await cloudinaryFileUploader({
file,
setCloudinaryData: setAccessibleCloudinaryData,
setProgressValue: setAccessibleProgressValue,
})
if (process.env.cloudinaryCloudName) {
await cloudinaryFileUploader({
file,
setCloudinaryData: setAccessibleCloudinaryData,
setProgressValue: setAccessibleProgressValue,
})
} else {
// TODO: Upload to AWS
alert("Cloudinary environment variables not set, must configure AWS")
}
}

const buildPreviewTableRow = (data: CloudinaryData, onDelete: () => void): StandardTableData => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,15 @@ describe("ListingViewSeedsHelpers", () => {
})

describe("getMarketingFlyers", () => {
const oldEnv = process.env

beforeEach(() => {
process.env = { ...oldEnv }
})

afterAll(() => {
process.env = oldEnv
})
it("should return empty array when feature flag is disabled", () => {
const mockJurisdiction = {
...jurisdiction,
Expand Down Expand Up @@ -622,6 +631,7 @@ describe("ListingViewSeedsHelpers", () => {
})

it("should use cloudinary URL when file asset exists", () => {
process.env.CLOUDINARY_CLOUD_NAME = "exygy"
const mockJurisdiction = {
...jurisdiction,
featureFlags: [
Expand All @@ -642,9 +652,7 @@ describe("ListingViewSeedsHelpers", () => {

const result = getMarketingFlyers(mockListing, mockJurisdiction)
expect(result).toHaveLength(1)
expect(result[0].url).toBe(
"https://res.cloudinary.com/undefined/image/upload/test-file-id.pdf"
)
expect(result[0].url).toBe("https://res.cloudinary.com/exygy/image/upload/test-file-id.pdf")
})

it("should return empty array when no flyers exist", () => {
Expand Down Expand Up @@ -779,7 +787,17 @@ describe("ListingViewSeedsHelpers", () => {
})

describe("getBuildingSelectionCriteria", () => {
const oldEnv = process.env

beforeEach(() => {
process.env = { ...oldEnv }
})

afterAll(() => {
process.env = oldEnv
})
it("should return link when listingsBuildingSelectionCriteriaFile exists", () => {
process.env.CLOUDINARY_CLOUD_NAME = "exygy"
const mockListing: Listing = {
...listing,
listingsBuildingSelectionCriteriaFile: {
Expand All @@ -791,7 +809,7 @@ describe("ListingViewSeedsHelpers", () => {
const result = getBuildingSelectionCriteria(mockListing)

expect(result.props.children.props.href).toEqual(
"https://res.cloudinary.com/undefined/image/upload/test-file-id.pdf"
"https://res.cloudinary.com/exygy/image/upload/test-file-id.pdf"
)
})

Expand Down
Loading