Store tags in field on contract. Script to update contract tags
This commit is contained in:
parent
db5e4af3bf
commit
3f6c9703d9
|
@ -9,6 +9,7 @@ export type Contract = {
|
|||
|
||||
question: string
|
||||
description: string // More info about what the contract is about
|
||||
tags: string[]
|
||||
outcomeType: 'BINARY' // | 'MULTI' | 'interval' | 'date'
|
||||
// outcomes: ['YES', 'NO']
|
||||
visibility: 'public' | 'unlisted'
|
||||
|
|
|
@ -2,6 +2,7 @@ import { calcStartPool } from './antes'
|
|||
|
||||
import { Contract } from './contract'
|
||||
import { User } from './user'
|
||||
import { parseTags } from './util/parse'
|
||||
|
||||
export function getNewContract(
|
||||
id: string,
|
||||
|
@ -28,6 +29,7 @@ export function getNewContract(
|
|||
|
||||
question: question.trim(),
|
||||
description: description.trim(),
|
||||
tags: parseTags(`${question} ${description}`),
|
||||
visibility: 'public',
|
||||
|
||||
mechanism: 'dpm-2',
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import _ from 'lodash'
|
||||
|
||||
export function parseTags(text: string) {
|
||||
const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi
|
||||
const matches = (text.match(regex) || []).map((match) =>
|
||||
match.trim().substring(1)
|
||||
)
|
||||
return _.uniqBy(matches, (tag) => tag.toLowerCase())
|
||||
const tagSet = new Set(matches)
|
||||
const uniqueTags: string[] = []
|
||||
tagSet.forEach((tag) => uniqueTags.push(tag))
|
||||
return uniqueTags
|
||||
}
|
49
functions/src/scripts/update-contract-tags.ts
Normal file
49
functions/src/scripts/update-contract-tags.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
import * as admin from 'firebase-admin'
|
||||
import * as _ from 'lodash'
|
||||
|
||||
// Generate your own private key, and set the path below:
|
||||
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
|
||||
// James:
|
||||
const serviceAccount = require('/Users/jahooma/mantic-markets-firebase-adminsdk-1ep46-820891bb87.json')
|
||||
// Stephen:
|
||||
// const serviceAccount = require('../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json')
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
})
|
||||
const firestore = admin.firestore()
|
||||
|
||||
import { Contract } from '../../../common/contract'
|
||||
import { parseTags } from '../../../common/util/parse'
|
||||
import { getValues } from '../utils'
|
||||
|
||||
async function updateContractTags() {
|
||||
console.log('Updating contracts tags')
|
||||
|
||||
const contracts = await getValues<Contract>(firestore.collection('contracts'))
|
||||
|
||||
console.log('Loaded', contracts.length, 'contracts')
|
||||
|
||||
for (const contract of contracts) {
|
||||
const contractRef = firestore.doc(`contracts/${contract.id}`)
|
||||
|
||||
const tags = _.uniq([
|
||||
...parseTags(contract.question + contract.description),
|
||||
...(contract.tags ?? []),
|
||||
])
|
||||
|
||||
console.log(
|
||||
'Updating tags',
|
||||
contract.slug,
|
||||
'from',
|
||||
contract.tags,
|
||||
'to',
|
||||
tags
|
||||
)
|
||||
|
||||
await contractRef.update({
|
||||
tags,
|
||||
} as Partial<Contract>)
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) updateContractTags().then(() => process.exit())
|
|
@ -2,7 +2,6 @@
|
|||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedLocals": true,
|
||||
"outDir": "lib",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
|
|
|
@ -10,7 +10,7 @@ import {
|
|||
contractPath,
|
||||
} from '../lib/firebase/contracts'
|
||||
import { Col } from './layout/col'
|
||||
import { parseTags } from '../lib/util/parse'
|
||||
import { parseTags } from '../../common/util/parse'
|
||||
import dayjs from 'dayjs'
|
||||
import { TrendingUpIcon, ClockIcon } from '@heroicons/react/solid'
|
||||
import { DateTimeTooltip } from './datetime-tooltip'
|
||||
|
|
|
@ -11,7 +11,7 @@ import {
|
|||
import { User } from '../lib/firebase/users'
|
||||
import { Col } from './layout/col'
|
||||
import { SiteLink } from './site-link'
|
||||
import { parseTags } from '../lib/util/parse'
|
||||
import { parseTags } from '../../common/util/parse'
|
||||
import { ContractCard } from './contract-card'
|
||||
import { Sort, useQueryAndSortParams } from '../hooks/use-sort-and-query-params'
|
||||
|
||||
|
|
|
@ -13,8 +13,7 @@
|
|||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"noUnusedLocals": false
|
||||
"incremental": true
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "../common/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
|
|
Loading…
Reference in New Issue
Block a user