/home/bdqbpbxa/dev-subdomains/admin.pixory.goodface.com.ua/src/api/address/policies/is-owner.ts
import { Address } from "../common/types/Address";
import { errors } from "@strapi/utils";

const { PolicyError } = errors    

export default async (ctx, config, { strapi }) => {
  const userId = ctx.state.user?.id;
  const { id } = ctx.params;

  if (!userId) {
    return false
  }

  const address = await strapi.db.query("api::address.address").findOne({
    where: { id },
    populate: { user: true },
  }) as Address;

  if (!address || address.user?.id !== userId) {
    throw new PolicyError("You are not the owner of this address")
  }

  return true;
};