/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gnd.persistence.remote.firestore.schema;

import com.google.android.gnd.model.Project;
import com.google.android.gnd.model.User;
import com.google.android.gnd.persistence.remote.firestore.base.FluentCollectionReference;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FieldPath;
import io.reactivex.Single;
import java.util.List;

public class ProjectsCollectionReference extends FluentCollectionReference {
  private static final String ACL_FIELD = "acl";
  private static final String READ_ACCESS = "r";

  ProjectsCollectionReference(CollectionReference ref) {
    super(ref);
  }

  public ProjectDocumentReference project(String id) {
    return new ProjectDocumentReference(reference().document(id));
  }

  public Single<List<Project>> getReadable(User user) {
    return runQuery(
        reference().whereArrayContains(FieldPath.of(ACL_FIELD, user.getEmail()), READ_ACCESS),
        ProjectConverter::toProject);
  }
}

@@ -970,16 +970,41 @@
 Single;%0A
+import java.util.Arrays;%0A
 import j
@@ -1018,16 +1018,16 @@
 l.List;%0A
-
 %0Apublic 
@@ -1179,25 +1179,329 @@
 ing 
-READ_ACCESS = %22r%22
+OWNER_ROLE = %22owner%22;%0A  private static final String MANAGER_ROLE = %22manage%22;%0A  private static final String CONTRIBUTOR_ROLE = %22contributor%22;%0A  private static final String VIEWER_ROLE = %22viewer%22;%0A  private static final List%3CString%3E VALID_ROLES =%0A      Arrays.asList(OWNER_ROLE, MANAGER_ROLE, CONTRIBUTOR_ROLE, VIEWER_ROLE)
 ;%0A%0A 
@@ -1810,21 +1810,10 @@
 here
-ArrayContains
+In
 (Fie
@@ -1855,18 +1855,18 @@
 )), 
-READ_ACCES
+VALID_ROLE
 S),%0A

Update project acls to new remote db structure