Match lowercase tags for folds

This commit is contained in:
James Grugett 2022-02-01 12:29:14 -06:00
parent 96c1410f01
commit 7c50b55331
4 changed files with 41 additions and 2 deletions

View File

@ -7,6 +7,7 @@ export type Fold = {
createdTime: number
tags: string[]
lowercaseTags: string[]
contractIds: string[]
excludedContractIds: string[]

View File

@ -59,6 +59,7 @@ export const createFold = functions.runWith({ minInstances: 1 }).https.onCall(
name,
about,
tags,
lowercaseTags: tags.map((tag) => tag.toLowerCase()),
createdTime: Date.now(),
contractIds: [],
excludedContractIds: [],

View File

@ -0,0 +1,34 @@
import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('james')
import { getValues } from '../utils'
import { Fold } from '../../../common/fold'
async function lowercaseFoldTags() {
const firestore = admin.firestore()
console.log('Updating fold tags')
const folds = await getValues<Fold>(firestore.collection('folds'))
console.log('Loaded', folds.length, 'folds')
for (const fold of folds) {
const foldRef = firestore.doc(`folds/${fold.id}`)
const { tags } = fold
const lowercaseTags = _.uniq(tags.map((tag) => tag.toLowerCase()))
console.log('Adding lowercase tags', fold.slug, lowercaseTags)
await foldRef.update({
lowercaseTags,
} as Partial<Fold>)
}
}
if (require.main === module) {
lowercaseFoldTags().then(() => process.exit())
}

View File

@ -135,9 +135,12 @@ export function listenForFollow(
export function getFoldsByTags(tags: string[]) {
if (tags.length === 0) return []
const lowercaseTags = tags.map((tag) => tag)
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
return getValues<Fold>(
// TODO: split into multiple queries if tags.length > 10.
query(foldCollection, where('tags', 'array-contains-any', lowercaseTags))
query(
foldCollection,
where('lowercaseTags', 'array-contains-any', lowercaseTags)
)
)
}