Skip to content

Commit

Permalink
Fix get items not translated
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciabad committed Oct 25, 2023
1 parent f8140e2 commit 6725cd0
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/server/api/router/places.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import 'server-only'

import { asc, eq, sql } from 'drizzle-orm'
import { and, asc, eq, notExists, or, sql } from 'drizzle-orm'
import { db } from '~/server/db/db'
import { placesData, placesTranslations } from '~/server/db/schema/places'
import { procedure, router } from '~/server/trpc'
import { listPlacesSchema } from '~/schemas/places'
import { defaultLocale } from '~/i18n'

// TODO: Handle when a specific place doesn't have a translation for the given locale while others do.
// Right now what happens is that the place without the locale is not returned in the list
const getAllPlaces = db
.select({
id: placesData.id,
mainImage: placesData.mainImage,
name: placesTranslations.name,
})
.from(placesData)
.innerJoin(placesTranslations, eq(placesData.id, placesTranslations.placeId))
.where(eq(placesTranslations.locale, sql.placeholder('locale')))
.leftJoin(placesTranslations, eq(placesData.id, placesTranslations.placeId))
.where(
or(
eq(placesTranslations.locale, sql.placeholder('locale')),
and(
eq(placesTranslations.locale, defaultLocale),
notExists(
db
.select()
.from(placesTranslations)
.where(
and(
eq(placesTranslations.placeId, placesData.id),
eq(placesTranslations.locale, sql.placeholder('locale'))
)
)
)
)
)
)
.orderBy(asc(placesTranslations.name))
.prepare()

Expand Down

0 comments on commit 6725cd0

Please sign in to comment.