Skip to content

Commit

Permalink
Merge branch 'dev' into l10n_dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Feb 23, 2024
2 parents 9cdfac0 + fc96361 commit bf70936
Show file tree
Hide file tree
Showing 15 changed files with 1,654 additions and 113 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@
},
"patchedDependencies": {
"@crowdin/[email protected]": "patches/@[email protected]",
"[email protected]": "patches/[email protected]"
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]"
},
"peerDependencyRules": {
"allowedVersions": {
Expand Down
87 changes: 49 additions & 38 deletions packages/api/router/organization/query.searchDistance.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,38 +105,52 @@ services AS (
),
service_area as (
SELECT
country."geoDataId" as "countryGeoId",
district."geoDataId" as "districtGeoId",
district.slug AS "districtSlug",
country.cca3 as "cca3",
sa."organizationId",
sa."orgLocationId"
(CASE
WHEN sa."organizationId" IS NOT NULL THEN sa."organizationId"
WHEN sa."orgLocationId" IS NOT NULL THEN (SELECT "orgId" FROM "OrgLocation" loc WHERE loc.id = sa."orgLocationId" )
WHEN sa."orgServiceId" IS NOT NULL THEN COALESCE((SELECT DISTINCT loc."orgId" FROM "OrgLocationService" ols LEFT JOIN "OrgLocation" loc ON ols."orgLocationId" = loc.id WHERE ols."serviceId" = sa."orgServiceId"),
(SELECT os."organizationId" FROM "OrgService" os WHERE os.id = sa."orgServiceId")
)
END
) AS "orgId",
ARRAY_agg(DISTINCT CASE
WHEN country."geoDataId" IS NOT NULL THEN country."geoDataId"
WHEN district."geoDataId" IS NOT NULL THEN district."geoDataId"
END) AS "geoId",
array_remove(array_agg(DISTINCT district.slug), NULL) AS "matchedDistricts",
array_remove(array_agg(DISTINCT country.cca3),NULL) AS "matchedCountries"
FROM "ServiceArea" sa
LEFT JOIN "ServiceAreaCountry" sac ON sac. "serviceAreaId" = sa.id AND sac.active
LEFT JOIN "ServiceAreaDist" sad ON sad. "serviceAreaId" = sa.id AND sad.active
LEFT JOIN "Country" country ON country.id = sac. "countryId"
LEFT JOIN "GovDist" district ON district.id = sad. "govDistId"
LEFT JOIN "ServiceAreaCountry" sac ON sac. "serviceAreaId" = sa.id
AND sac.active
LEFT JOIN "ServiceAreaDist" sad ON sad. "serviceAreaId" = sa.id
AND sad.active
LEFT JOIN "Country" country ON country.id = sac. "countryId" AND country. "geoDataId" = ANY(
SELECT id
FROM covered_areas
)
LEFT JOIN "GovDist" district ON district.id = sad. "govDistId" AND district. "geoDataId" = ANY(
SELECT id
FROM covered_areas
)
WHERE sa.active
AND sa."organizationId" is not null
AND (
country."geoDataId" is not null
OR district."geoDataId" is not null
country. "geoDataId" = ANY(
SELECT id
FROM covered_areas
)
AND (country."geoDataId" = ANY(
SELECT id
FROM covered_areas
)
OR district."geoDataId" = ANY(
SELECT id
FROM covered_areas
))
OR district. "geoDataId" = ANY(
SELECT id
FROM covered_areas
)
)
GROUP BY "orgId"
)
SELECT
*,
COUNT(*) OVER ()::int AS total
FROM (
SELECT
loc. "orgId",
org.id,
${
hasServiceFilter
? Prisma.sql`ARRAY_REMOVE(ARRAY_AGG(DISTINCT services. "tagId"), NULL) AS "matchedServices",`
Expand All @@ -155,16 +169,14 @@ service_area as (
ST_Distance(ST_Transform(loc.geo, 3857), (SELECT meters FROM points))::int
)
) AS distance,
ARRAY_REMOVE(ARRAY_AGG(DISTINCT sa.cca3), NULL) AS "national",
ARRAY_LENGTH(ARRAY_REMOVE(ARRAY_AGG(DISTINCT sa.cca3), NULL),1) is not NULL AS "isNational",
ARRAY_REMOVE(ARRAY_AGG(DISTINCT sa.cca3) || ARRAY_AGG( DISTINCT sa."districtSlug"), NULL) AS "serviceAreas"
FROM "OrgLocation" loc
INNER JOIN "Organization" org ON org.id = loc. "orgId"
LEFT JOIN service_area sa ON sa. "organizationId" = loc. "orgId"
sa."matchedCountries" AS "national"
FROM "Organization" org
INNER JOIN "OrgLocation" loc ON org.id = loc. "orgId"
LEFT JOIN service_area sa ON sa. "orgId" = org.id
${
hasServiceFilter
? Prisma.sql`
INNER JOIN services ON services."organizationId" = loc."orgId"`
INNER JOIN services ON services."organizationId" = org.id`
: Prisma.empty
}
${
Expand All @@ -176,15 +188,14 @@ service_area as (
WHERE
(
ST_DWithin(ST_Transform(loc.geo, 3857), (SELECT meters FROM points), ${searchRadius})
OR sa."countryGeoId" = ANY(SELECT id FROM covered_areas)
OR sa."districtGeoId" = ANY(SELECT id FROM covered_areas)
OR sa."geoId" && ARRAY(SELECT id FROM covered_areas)
)
AND loc.published
AND org.published
AND NOT loc.deleted
AND NOT org.deleted
GROUP BY
loc."orgId"
org.id, sa."matchedCountries"
ORDER BY
distance
) result
Expand All @@ -197,21 +208,21 @@ OFFSET ${skip}`
const formattedResults = results.map((result) => {
if (parseInt(result.total) !== total) total = parseInt(result.total)
return {
id: result.orgId,
id: result.id,
distMeters: parseInt(result.distance),
national: result.national,
national: result.national ?? [],
}
})
return { results: formattedResults, total }
}
type SearchResult = {
orgId: string
id: string
matchedServices?: string[]
matchedAttributes?: string[]
distance: string
national: string[]
isNational: boolean
serviceAreas: string[]
national: string[] | null
// isNational: boolean
// serviceAreas: string[]
total: string
}
const prismaDistSearchDetails = async (input: TSearchDistanceSchema & { resultIds: string[] }) => {
Expand Down
105 changes: 31 additions & 74 deletions packages/db/lib/generateFreeText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,92 +49,49 @@ export const generateFreeText = <T extends GenerateFreeTextType>({
}),
}
}
export const generateNestedFreeText = <T extends GenerateFreeTextType>({
orgId: orgSlug,
itemId,
text,
type,
freeTextId,
}: GenerateFreeTextParams<T>) => {
const key = (() => {
switch (type) {
case 'orgDesc': {
return createKey([orgSlug, 'description'])
}
case 'attSupp': {
invariant(itemId)
return createKey([orgSlug, 'attribute', itemId])
}
case 'svcName': {
invariant(itemId)
return createKey([orgSlug, itemId, 'name'])
}
case 'websiteDesc':
case 'phoneDesc':
case 'emailDesc':
case 'svcDesc': {
invariant(itemId)
return createKey([orgSlug, itemId, 'description'])
}
}
})()
invariant(key, 'Error creating key')
const ns = namespaces.orgData
export const generateNestedFreeText = <T extends GenerateFreeTextType>(args: GenerateFreeTextParams<T>) => {
const { freeText, translationKey } = generateFreeText(args)
return {
create: {
id: freeTextId ?? generateId('freeText'),
tsKey: { create: { key, text, namespace: { connect: { name: ns } } } },
id: freeText.id,
tsKey: {
create: {
key: translationKey.key,
text: translationKey.text,
namespace: { connect: { name: translationKey.ns } },
},
},
},
}
}

export const generateNestedFreeTextUpsert = <T extends GenerateFreeTextType>({
orgId: orgSlug,
itemId,
text,
type,
freeTextId,
}: GenerateFreeTextParams<T>) => {
const key = (() => {
switch (type) {
case 'orgDesc': {
return createKey([orgSlug, 'description'])
}
case 'attSupp': {
invariant(itemId)
return createKey([orgSlug, 'attribute', itemId])
}
case 'svcName': {
invariant(itemId)
return createKey([orgSlug, itemId, 'name'])
}
case 'websiteDesc':
case 'phoneDesc':
case 'emailDesc':
case 'svcDesc': {
invariant(itemId)
return createKey([orgSlug, itemId, 'description'])
}
}
})()
invariant(key, 'Error creating key')
const ns = namespaces.orgData

const id = freeTextId ?? generateId('freeText')
export const generateNestedFreeTextUpsert = <T extends GenerateFreeTextType>(
args: GenerateFreeTextParams<T>
): Prisma.FreeTextUpdateOneWithoutOrgEmailNestedInput => {
const { freeText, translationKey } = generateFreeText(args)
return {
upsert: {
where: { id },
// where: { id: freeText.id },
create: {
id,
tsKey: { create: { key, text, namespace: { connect: { name: ns } } } },
id: freeText.id,
tsKey: {
create: {
key: translationKey.key,
text: translationKey.text,
namespace: { connect: { name: translationKey.ns } },
},
},
},
update: {
tsKey: {
upsert: {
where: { key },
create: { key, text, namespace: { connect: { name: ns } } },
update: { text },
},
// upsert: {
// create: {
// key: translationKey.key,
// text: translationKey.text,
// namespace: { connect: { name: translationKey.ns } },
// },
update: { text: translationKey.text },
// },
},
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"prisma-query-inspector": "1.4.4",
"prisma-query-log": "3.2.0",
"slugify": "1.6.6",
"social-links": "1.14.0",
"sql-bricks-postgres": "0.6.0",
"string-byte-length": "3.0.0",
"tiny-invariant": "1.3.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { prisma } from '~db/client'
import { isIdFor } from '~db/index'
import { formatMessage } from '~db/prisma/common'
import { type MigrationJob } from '~db/prisma/dataMigrationRunner'
import { createLogger, type JobDef, jobPostRunner } from '~db/prisma/jobPreRun'

/** Define the job metadata here. */
const jobDef: JobDef = {
jobId: '2024-02-21_attach-orphan-text',
title: 'attach orphan text',
createdBy: 'Joe Karow',
/** Optional: Longer description for the job */
description: undefined,
}
/**
* Job export - this variable MUST be UNIQUE
*/
export const job20240221_attach_orphan_text = {
title: `[${jobDef.jobId}] ${jobDef.title}`,
task: async (_ctx, task) => {
/** Create logging instance */
createLogger(task, jobDef.jobId)
const log = (...args: Parameters<typeof formatMessage>) => (task.output = formatMessage(...args))
/**
* Start defining your data migration from here.
*
* To log output, use `task.output = 'Message to log'`
*
* This will be written to `stdout` and to a log file in `/prisma/migration-logs/`
*/

// Do stuff

const data = await prisma.freeText.findMany({
where: {
AND: [
{ AttributeSupplement: null },
{ Organization: null },
{ OrgEmail: null },
{ OrgPhone: null },
{ OrgService: null },
{ OrgLocation: null },
{ OrgServiceName: null },
{ OrgWebsite: null },
],
},
})
await prisma.$transaction(
async (tx) => {
for (const item of data) {
const recordToAttachTo = item.key.split('.')[1]
if (typeof recordToAttachTo !== 'string') {
throw new Error('Unable to get record to attach to')
}
if (isIdFor('orgPhone', recordToAttachTo)) {
const result = await tx.orgPhone.update({
where: { id: recordToAttachTo },
data: { descriptionId: item.id },
})
log(`Attached orphan text ${item.key} to ${result.id}`)
} else if (isIdFor('orgEmail', recordToAttachTo)) {
const result = await tx.orgEmail.update({
where: { id: recordToAttachTo },
data: { descriptionId: item.id },
})
log(`Attached orphan text ${item.key} to ${result.id}`)
}
}
},
{ timeout: 180_000 }
)

/**
* DO NOT REMOVE BELOW
*
* This writes a record to the DB to register that this migration has run successfully.
*/
await jobPostRunner(jobDef)
},
def: jobDef,
} satisfies MigrationJob
Loading

0 comments on commit bf70936

Please sign in to comment.