GitFOSS
.ts
TypeScript
(application/typescript)
// 1st-party
import type { ServiceMethodFactory } from "@ethicdevs/react-monolith";
// generated via script[generate:prisma]
import { Organization } from "@prisma/client";
// app
import type { UsersServiceDeps } from "./types";

const getUserOrganizations: ServiceMethodFactory<
  UsersServiceDeps,
  [string],
  Promise<Organization[]>
> = ({ request }) => {
  return async (userId) => {
    const userOrgs = await request.prisma.organization.findMany({
      where: {
        OR: [
          {
            ownerId: userId,
          },
          {
            memberships: {
              some: {
                userId,
              },
            },
          },
        ],
      },
    });

    return userOrgs;
  };
};

export default getUserOrganizations;