Skip to content

Commit

Permalink
fixes isowner condition
Browse files Browse the repository at this point in the history
  • Loading branch information
rfontanarosa committed Dec 20, 2024
1 parent 9139f77 commit 25b3085
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
6 changes: 3 additions & 3 deletions functions/src/common/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class Datastore {
async fetchLoisSubmissions(
surveyId: string,
jobId: string,
userId: string | undefined,
ownerId: string | undefined,
page: number
) {
const loisQuery = this.db_
Expand All @@ -163,8 +163,8 @@ export class Datastore {
.collection(submissions(surveyId))
.where(sb.jobId, '==', jobId)
.orderBy(sb.loiId);
if (userId) {
submissionsQuery = submissionsQuery.where(sb.ownerId, '==', userId);
if (ownerId) {
submissionsQuery = submissionsQuery.where(sb.ownerId, '==', ownerId);
}
const loisIterator = new QueryIterator(loisQuery, page, l.id);
const submissionsIterator = new QueryIterator(
Expand Down
4 changes: 2 additions & 2 deletions functions/src/common/query-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class QueryIterator implements AsyncIterator<QueryDocumentSnapshot> {
async next(): Promise<IteratorResult<QueryDocumentSnapshot>> {
if (
this.querySnapshot === null ||
this.currentIndex >= this.querySnapshot.docs.length
this.currentIndex >= this.querySnapshot.size
) {
// Fetch next batch of documents
let q = this.query.limit(this.pageSize);
Expand All @@ -44,7 +44,7 @@ export class QueryIterator implements AsyncIterator<QueryDocumentSnapshot> {
this.querySnapshot = await q.get();
this.currentIndex = 0;
}
if (this.querySnapshot.docs.length > 0) {
if (this.querySnapshot.size > 0) {
const document = this.querySnapshot.docs[this.currentIndex++];
this.lastDocument = document; // Update last document for next batch
return {
Expand Down
20 changes: 8 additions & 12 deletions functions/src/export-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export async function exportCsvHandler(
const canManageSurvey = canImport(user, surveyDoc);
const ownerId = !canManageSurvey ? userId : undefined;

console.log(`Exporting survey '${surveyId}', job '${jobId}'`);
console.log(
`Exporting survey '${surveyId}', job '${jobId}', owner '${
ownerId || 'survey organizer'
}'`
);

const jobDoc = await db.fetchJob(surveyId, jobId);
if (!jobDoc.exists || !jobDoc.data()) {
Expand Down Expand Up @@ -88,12 +92,7 @@ export async function exportCsvHandler(
});
csvStream.pipe(res);

const rows = await db.fetchLoisSubmissions(
surveyId,
jobId,
!canManageSurvey ? userId : undefined,
50
);
const rows = await db.fetchLoisSubmissions(surveyId, jobId, ownerId, 50);

for await (const row of rows) {
try {
Expand Down Expand Up @@ -189,11 +188,8 @@ function toWkt(geometry: Pb.IGeometry): string {
* Checks if a Location of Interest (LOI) is accessible to a given user.
*/
function isAccessibleLoi(loi: Pb.ILocationOfInterest, ownerId?: string) {
return (
loi.source === Pb.LocationOfInterest.Source.IMPORTED ||
(loi.source === Pb.LocationOfInterest.Source.FIELD_DATA &&
loi.ownerId === ownerId)
);
const isFieldData = loi.source === Pb.LocationOfInterest.Source.FIELD_DATA;
return ownerId ? isFieldData && loi.ownerId === ownerId : true;
}

/**
Expand Down

0 comments on commit 25b3085

Please sign in to comment.