Filter projects by access ("PUBLIC" / "PRIVATE")

I have following request

get<Project[]>(
            `/projects?page-size=50&page=${page}&archived=false&access=PUBLIC`
          )

Have 3 project at clockify 2 private 1 public


Need to filter based on project access column, but current request returns all projects
Other filters working f.e ‘billable=false’

Hi Anri,

It looks like you’re trying to filter projects based on their access level (PUBLIC/PRIVATE) using the Clockify API, but the filter isn’t working as expected. The current request seems to be returning all projects instead of just the public ones. Here’s a solution that might help you filter the projects correctly:

Problem:

The current API request you’re using:

javascript

Copy code

get<Project[]>(
  `/projects?page-size=50&page=${page}&archived=false&access=PUBLIC`
)

This request is returning all projects regardless of the access level.

Solution:

Clockify’s API might not directly support filtering projects by access level in the way you’re trying to use it. You may need to fetch all projects first and then filter them on the client side based on their access level.

Step-by-Step Guide:

  1. Fetch All Projects: First, retrieve all projects without applying the access filter.

javascript

Copy code

get<Project[]>(
  `/projects?page-size=50&page=${page}&archived=false`
)
  1. Filter Projects on Client Side: Once you have all the projects, you can filter them based on the access level in your code.

javascript

Copy code

async function fetchAndFilterProjects() {
  try {
    const response = await axios.get(`/projects?page-size=50&page=${page}&archived=false`);
    const projects = response.data;
    
    // Filter projects by access level
    const publicProjects = projects.filter(project => project.access === 'PUBLIC');
    
    // Do something with the filtered projects
    console.log(publicProjects);
  } catch (error) {
    console.error('Error fetching projects:', error);
  }
}

fetchAndFilterProjects();

Explanation:

  1. Fetching Projects: You fetch all projects without filtering by access level.
  2. Filtering Projects: After fetching the projects, you filter them on the client side to get only the public projects.

Full Example:

Here is the full example incorporating both fetching and filtering:

javascript

Copy code

import axios from 'axios';

async function fetchAndFilterProjects(page) {
  try {
    const response = await axios.get(`/projects?page-size=50&page=${page}&archived=false`);
    const projects = response.data;
    
    // Filter projects by access level
    const publicProjects = projects.filter(project => project.access === 'PUBLIC');
    
    // Do something with the filtered projects
    console.log(publicProjects);
  } catch (error) {
    console.error('Error fetching projects:', error);
  }
}

// Example usage
fetchAndFilterProjects(1);

Additional Tips:

  • Pagination: If you have a large number of projects, ensure you handle pagination properly by iterating through all pages.
  • Error Handling: Make sure to handle any errors that might occur during the API request to avoid issues in your application.

By following this approach, you should be able to filter projects based on their access level effectively. If you have any further questions or need additional assistance, feel free to ask!

Hope this helps!

1 Like

Thanks I have similar solution
I m using next js serverside with trpc

projects: SCProcedure.input(projects).query(async ({ input }) => {
    const { userId } = input;
    try {
      const userProject = await ClockifyRepository.getUserProjects(userId);
      const publicProjects = await ClockifyRepository.getPublicProjects();
      // Remove duplicates
      const projects = [...userProject, ...publicProjects].filter(
        (project, index, self) =>
          index === self.findIndex((p) => p.id === project.id)
      );
      return projects;
    } catch (error) {
      console.error(error);
      throw new TRPCError({ code: "NOT_FOUND" });
    }
  }),

thats my getUserProjects

async getUserProjects(userId) {
      const { data: data } = await http.get<Project[]>(
        `/projects?page-size=50&page=${1}&archived=false&users=${userId}`
      );
      return data;
    },

and getPublic projects

async getPublicProjects() {
      // here is api call get<Project[]>(
      //      `/projects?page-size=50&page=${page}&archived=false`
      //    )
      const publicProjects = data.filter((el) => el.public);
      return publicProjects;
    },

I needed to do it in one query, to fetch all public projects, and all other private projects where user have access.
Every query filter works what is described in clockify developer api docs, expect access filter
Thanks