Skip to content

Commit

Permalink
refactor: 🎨 Refactor getUserFixedTags and add edge case handling
Browse files Browse the repository at this point in the history
  • Loading branch information
linyc0817 committed Sep 15, 2023
1 parent fd448a3 commit fc85af6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 44 deletions.
72 changes: 44 additions & 28 deletions apollo/src/__test__/graphql_integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,18 @@ describe('test graphql query', () => {
longitude: '120.99719144686011',
},
};
const docData2 = {
groupId: -1,
locationName: '活動中心&一餐2',
coordinates: {
latitude: '24.789345225611136',
longitude: '120.99719144686011',
},
};
const docRef = await firestore.collection('fixedTag_research').add(docData);
const docRef2 = await firestore
.collection('fixedTag_research')
.add(docData2);

const collectionRef = firestore.collection('tagData_research');
const defaultStatus = {
Expand Down Expand Up @@ -804,34 +815,39 @@ describe('test graphql query', () => {
'getUserFixedTagResearchList',
{ uNumber: 11 }
);
expect(queryResult.fixedTags[0]).toHaveProperty('id', docRef.id);
expect(queryResult.fixedTags[0]).toHaveProperty(
'locationName',
docData.locationName
);
expect(queryResult.fixedTags[0]).toHaveProperty(
'coordinates',
docData.coordinates
);
const fixedTagSubTagsResult = {};
queryResult.fixedTags[0].tags.forEach(location => {
fixedTagSubTagsResult[location.id] = location;
});
// console.log(fixedTagSubTagsResult[tagDocRef.id].statusHistory);
const statusExpectData = {
statusName: '清潔狀態',
statusDescName: '乾淨',
createTime: expect.stringMatching(timestampStringRegex),
};
expect(fixedTagSubTagsResult[tagDocRef.id]).toMatchObject({
...tagData,
id: tagDocRef.id,
fixedTagId: docRef.id,
status: statusExpectData,
statusHistory: {
statusList: [statusExpectData],
},
});

if (queryResult.fixedTags.length < 2) {
expect(queryResult.fixedTags[0]).toHaveProperty('id', docRef2.id);
} else {
expect(queryResult.fixedTags[0]).toHaveProperty('id', docRef.id);
expect(queryResult.fixedTags[0]).toHaveProperty(
'locationName',
docData.locationName
);
expect(queryResult.fixedTags[0]).toHaveProperty(
'coordinates',
docData.coordinates
);
const fixedTagSubTagsResult = {};
queryResult.fixedTags[0].tags.forEach(location => {
fixedTagSubTagsResult[location.id] = location;
});
// console.log(fixedTagSubTagsResult[tagDocRef.id].statusHistory);
const statusExpectData = {
statusName: '清潔狀態',
statusDescName: '乾淨',
createTime: expect.stringMatching(timestampStringRegex),
};
expect(fixedTagSubTagsResult[tagDocRef.id]).toMatchObject({
...tagData,
id: tagDocRef.id,
fixedTagId: docRef.id,
status: statusExpectData,
statusHistory: {
statusList: [statusExpectData],
},
});
}

// also test one fixed tag query
const queryFixedTag = gql`
Expand Down
43 changes: 27 additions & 16 deletions apollo/src/datasources/TagResearchDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,32 @@ class TagResearchDataSource extends DataSource {
}

async getUserFixedTags(uNumber, pageParams) {
let query;
if (uNumber < 25) {
query = this.fixedTagResearchCollectionRef
.where('groupId', '==', 1)
.orderBy('__name__');
} else if (uNumber >= 25 && uNumber < 48) {
query = this.fixedTagResearchCollectionRef
.where('groupId', '==', 2)
.orderBy('__name__');
} else if (uNumber >= 48) {
query = this.fixedTagResearchCollectionRef
.where('groupId', '==', 3)
.orderBy('__name__');
// seperate users into three groups
const groupIds = {
group1: [1, -1],
group2: [2, -1],
group3: [3, -1],
groupDefault: [-1],
};

// get groupIds by uNumber
let selectedGroupIds;

if (uNumber < 25 && uNumber > 0) {
selectedGroupIds = groupIds.group1;
} else if (uNumber >= 25 && uNumber < 49) {
selectedGroupIds = groupIds.group2;
} else if (uNumber >= 49 && uNumber < 73) {
selectedGroupIds = groupIds.group3;
} else {
selectedGroupIds = groupIds.groupDefault;
}

// get query by groupIds
const query = this.fixedTagResearchCollectionRef
.where('groupId', 'in', selectedGroupIds)
.orderBy('__name__');

const { data: fixedTags, pageInfo } = await getPage(
query,
pageParams,
Expand All @@ -146,9 +158,8 @@ class TagResearchDataSource extends DataSource {
.doc(fixedTagId)
.get();
const fixedTagLocation = docSnap.data().locationName;
const matchingLocation = locationNameToCoordinates.find(
// location => location.locationName === fixedTagLocation
location => fixedTagLocation.includes(location.locationName)
const matchingLocation = locationNameToCoordinates.find(location =>
fixedTagLocation.includes(location.locationName)
);
// for the location missing
if (!matchingLocation) {
Expand Down

0 comments on commit fc85af6

Please sign in to comment.