Update group field names

This commit is contained in:
Ian Philips 2022-09-02 16:35:58 -06:00
parent 4d3dcdaf72
commit 57f59ebfc4
7 changed files with 30 additions and 14 deletions

View File

@ -7,8 +7,8 @@ export type Group = {
createdTime: number createdTime: number
mostRecentActivityTime: number mostRecentActivityTime: number
anyoneCanJoin: boolean anyoneCanJoin: boolean
numContracts: number totalContracts: number
numMembers: number totalMembers: number
aboutPostId?: string aboutPostId?: string
chatDisabled?: boolean chatDisabled?: boolean
mostRecentContractAddedTime?: number mostRecentContractAddedTime?: number

View File

@ -59,8 +59,8 @@ export const creategroup = newEndpoint({}, async (req, auth) => {
mostRecentActivityTime: Date.now(), mostRecentActivityTime: Date.now(),
// TODO: allow users to add contract ids on group creation // TODO: allow users to add contract ids on group creation
anyoneCanJoin, anyoneCanJoin,
numContracts: 0, totalContracts: 0,
numMembers: memberIds.length, totalMembers: memberIds.length,
} }
await groupRef.create(group) await groupRef.create(group)

View File

@ -31,7 +31,7 @@ export const onCreateGroupContract = functions.firestore
.doc(groupId) .doc(groupId)
.update({ .update({
mostRecentContractAddedTime: Date.now(), mostRecentContractAddedTime: Date.now(),
numContracts: admin.firestore.FieldValue.increment(1), totalContracts: admin.firestore.FieldValue.increment(1),
}) })
}) })
@ -45,7 +45,7 @@ export const onDeleteGroupContract = functions.firestore
.doc(groupId) .doc(groupId)
.update({ .update({
mostRecentContractAddedTime: Date.now(), mostRecentContractAddedTime: Date.now(),
numContracts: admin.firestore.FieldValue.increment(-1), totalContracts: admin.firestore.FieldValue.increment(-1),
}) })
}) })
@ -59,7 +59,7 @@ export const onCreateGroupMember = functions.firestore
.doc(groupId) .doc(groupId)
.update({ .update({
mostRecentActivityTime: Date.now(), mostRecentActivityTime: Date.now(),
numMembers: admin.firestore.FieldValue.increment(1), totalMembers: admin.firestore.FieldValue.increment(1),
}) })
}) })
@ -73,7 +73,7 @@ export const onDeleteGroupMember = functions.firestore
.doc(groupId) .doc(groupId)
.update({ .update({
mostRecentActivityTime: Date.now(), mostRecentActivityTime: Date.now(),
numMembers: admin.firestore.FieldValue.increment(-1), totalMembers: admin.firestore.FieldValue.increment(-1),
}) })
}) })

View File

@ -39,8 +39,8 @@ const createGroup = async (
createdTime: now, createdTime: now,
mostRecentActivityTime: now, mostRecentActivityTime: now,
anyoneCanJoin: true, anyoneCanJoin: true,
numContracts: contracts.length, totalContracts: contracts.length,
numMembers: 1, totalMembers: 1,
} }
await groupRef.create(group) await groupRef.create(group)
// create a GroupMemberDoc for the creator // create a GroupMemberDoc for the creator

View File

@ -67,7 +67,23 @@ const convertGroupFieldsToGroupDocuments = async () => {
} }
} }
const updateTotalContractsAndMembers = async () => {
const groups = await getGroups()
for (const group of groups) {
log('updating group total contracts and members', group.slug)
const groupRef = admin.firestore().collection('groups').doc(group.id)
const totalMembers = (await groupRef.collection('groupMembers').get()).size
const totalContracts = (await groupRef.collection('groupContracts').get())
.size
await groupRef.update({
totalMembers,
totalContracts,
})
}
}
if (require.main === module) { if (require.main === module) {
initAdmin() initAdmin()
convertGroupFieldsToGroupDocuments() // convertGroupFieldsToGroupDocuments()
updateTotalContractsAndMembers()
} }

View File

@ -282,8 +282,8 @@ function ContractSearchControls(props: {
: DEFAULT_CATEGORY_GROUPS.map((g) => g.slug) : DEFAULT_CATEGORY_GROUPS.map((g) => g.slug)
const memberPillGroups = sortBy( const memberPillGroups = sortBy(
memberGroups.filter((group) => group.numContracts > 0), memberGroups.filter((group) => group.totalContracts > 0),
(group) => group.numContracts (group) => group.totalContracts
).reverse() ).reverse()
const pillGroups: { name: string; slug: string }[] = const pillGroups: { name: string; slug: string }[] =

View File

@ -122,7 +122,7 @@ export async function getStaticProps() {
const markets = Object.fromEntries(groups.map((g, i) => [g.id, contracts[i]])) const markets = Object.fromEntries(groups.map((g, i) => [g.id, contracts[i]]))
const groupMap = keyBy(groups, 'id') const groupMap = keyBy(groups, 'id')
const numPeople = mapValues(groupMap, (g) => g?.numMembers) const numPeople = mapValues(groupMap, (g) => g?.totalMembers)
const slugs = mapValues(groupMap, 'slug') const slugs = mapValues(groupMap, 'slug')
return { props: { markets, numPeople, slugs }, revalidate: 60 * 10 } return { props: { markets, numPeople, slugs }, revalidate: 60 * 10 }