Hello from MCP server

List Files | Just Commands | Repo | Logs

← back |
import readline from "readline";
import PocketBase from "pocketbase";

export function getPb(host) {
  if (host == undefined) {
    host = "http://localhost:8090";
  }

  const pb = new PocketBase(host);
  pb.autoCancellation(false);
  return pb;
}

export const waitMs = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

export async function wait(msg) {
  if (msg == undefined) {
    msg = "Press Enter to  continue...\n";
  }
  await new Promise((resolve) => {
    const rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout,
    });

    rl.question(msg, () => {
      rl.close();
      resolve();
    });
  });
}

export const users = {
  buck: {
    name: "Buck Strickland",
    email: "buck@stricklandpropane.com",
    password: "sugarfoots",
    org: "Strickland Propane",
    orgId: "",
  },
  hank: {
    name: "Hank Hill",
    email: "hank@stricklandpropane.com",
    password: "ladybird123",
  },
  joejack: {
    name: "Joe Jack",
    email: "joejack@stricklandpropane.com",
    password: "tacobuenohoney",
  },
  debbie: {
    name: "Debbie Grund",
    email: "debiie@stricklandpropane.com",
    password: "hatemizliz",
  },

  peggy: {
    name: "Peggy Hill",
    email: "yohabloespanol@tomlandrymiddleschool.com",
    password: "woohaaaa!!",
  },
  bobby: {
    name: "Bobby Hill",
    email: "pegaleg@yahoo.com",
    password: "bobby",
  },
  thatherton: {
    name: "M.F. Thatherton",
    email: "mf@thathertonfuels.com",
    password: "1234567890",
    org: "Thatherton Fuels",
    orgId: "",
  },
  bender: {
    name: "Bender Bending Rodriguez",
    email: "bender@planetexpress.com",
    password: "benderisgreat",
    org: "Bender's Friendly Spaceship Repair",
    orgId: "",
    orgCurrency: "NixonBucks",
    bookId: "",
    tiersId: "",
    costsId: "",
  },
  scruffy: {
    name: "Scruffy",
    email: "scruffy@planetexpress.com",
    password: "scruffyscruffy",
    org: "Scruffy's Space Shop",
    orgId: "",
    orgCurrency: "Slurm Credits",
    bookId: "",
  },
  zoidberg: {
    name: "Zoidberg",
    email: "zoidberg@planetexpress.com",
    password: "blublublublublublub",
    org: "Why can't I fix a spaceship?",
    orgId: "",
    orgCurrency: "Slurm Credits",
    bookId: "",
  },
  amy: {
    name: "Amy Wong",
    email: "amy@planetexpress.com",
    password: "kifforever",
  },
  leela: {
    name: "Turanga Leela",
    email: "leela@planetexpress.com",
    password: "nibblerinboots",
  },
};

const verbose = false;

export async function check(fn, msg) {
  try {
    const r = await fn();
    console.log(`${msg} ✅`);

    if (verbose) {
      console.log(r);
    }
  } catch (e) {
    console.log(`${msg} ❌`);
    console.log(e);
  }
}

export function getApi(pb) {
  return {
    pb,
    register: async function (user) {
      const data = {
        name: user.name,
        password: user.password,
        passwordConfirm: user.password,
        email: user.email,
        emailVisibility: true,
      };
      return await this.pb.collection("users").create(data);
    },
    login: async function (user) {
      return await this.pb
        .collection("users")
        .authWithPassword(user.email, user.password);
    },
    createOrg: async function (user, variables = null) {
      await this.login(user);
      const data = {
        name: user.org,
        owner: this.pb.authStore.record.id,
      };
      if (variables) {
        data.variables = variables;
      }
      return await this.pb.collection("organizations").create(data);
    },
    viewOrg: async function (user, orgId) {
      await this.login(user);
      await this.pb.collection("organizations").getOne(orgId);
    },
    joinOrg: async function (user, orgId) {
      await this.login(user);
      return await this.pb.send("/api/join", {
        method: "POST",
        body: JSON.stringify({
          orgId: orgId,
          userId: this.pb.authStore.record.id,
        }),
      });
    },
    listOrgRoles: async function (user) {
      await this.login(user);
      return await this.pb.collection("roles").getFullList();
    },
    listOrgProfiles: async function (user) {
      await this.login(user);
      return await this.pb.collection("profiles").getFullList({
        expand: "user",
      });
    },
    getOrgProfile: async function (user, targetUser) {
      await this.login(user);
      console.log("**", this.pb.authStore.record.activeOrg);
      console.log(targetUser);
      const profiles = await this.pb.collection("profiles").getFullList({
        filter: `org = "${this.pb.authStore.record.activeOrg}"`,
        expand: "user, roles",
      });
      return profiles.find((e) => e.expand?.user?.name == targetUser.name);
    },
    removeFromOrg: async function (user, targetUser) {
      await this.login(user);
      const toRemove = await this.getOrgProfile(user, targetUser);
      await this.pb.collection("profiles").delete(toRemove.id);
    },
    assignRole: async function (user, targetUser, roleName) {
      await this.login(user);
      const profile = await this.getOrgProfile(user, targetUser);
      const roles = await this.pb.collection("roles").getFullList();
      const role = roles.find((e) => e.name == roleName);
      profile.roles.push(role.id);
      return await this.pb
        .collection("profiles")
        .update(profile.id, { roles: profile.roles });
    },
    publishChanges: async function (user, orgId, bookId, changes) {
      await this.login(user);
      await this.pb.collection("pricebookChanges").create({
        org: orgId,
        book: bookId,
        changeset: changes,
      });
    },
    createBook: async function (
      user,
      orgId,
      name,
      refId,
      parent,
      dependencies,
    ) {
      await this.login(user);
      return await this.pb.collection("books").create({
        name,
        refId,
        org: orgId,
        parent,
        dependencies,
      });
    },
    createMenu: async function (user, orgId, bookId, name) {
      await this.login(user);
      return await this.pb.collection("menus").create({
        name,
        org: orgId,
        books: [bookId],
      });
    },
    createOffer: async function (user, orgId, menuId, name, refId) {
      await this.login(user);
      return await this.pb.collection("offers").create({
        name,
        org: orgId,
        menus: [menuId],
        refId: refId,
      });
    },
    chooseCurrency: async function (user, orgId, currencyName) {
      await this.login(user);
      // const c = await pb.collection("currencies").getFullList();
      // console.log(c);
      const currency = await this.pb
        .collection("currencies")
        .getFirstListItem(`name="${currencyName}"`);
      await this.pb
        .collection("organizations")
        .update(orgId, { currency: currency.id });
    },
    listCurrencies: async function (user) {
      await this.login(user);
      return await this.pb.collection("currencies").getFullList();
    },
    createCostTime: async function (user, orgId, refId, hours) {
      await this.login(user);
      await this.pb.collection("costsTime").create({
        name: refId,
        refId,
        hours,
        orgId,
      });
    },
    addSubscriber: async function (user, bookId, orgId) {
      await this.login(user);
      const book = await this.pb.collection("books").getOne(bookId);
      book.subscribers.push(orgId);
      const data = {
        subscribers: book.subscribers,
      };
      await this.pb.collection("books").update(bookId, data);
    },
    listBooks: async function (user) {
      await this.login(user);
      return await this.pb.collection("books").getFullList();
    },
    viewBook: async function (user, bookId) {
      await this.login(user);
      return await this.pb.collection("books").getOne(bookId);
    },
    listChanges: async function (user) {
      await this.login(user);
      return await this.pb.collection("pricebookChanges").getFullList();
    },
    listWarranties: async function (user) {
      await this.login(user);
      return await this.pb.collection("warrantyCopy").getFullList();
    },
    listMenus: async function (user) {
      await this.login(user);
      return await this.pb.collection("menus").getFullList();
    },
    updateUserActiveOrg: async function (user, newActiveOrgId) {
      await this.login(user);
      return await this.pb
        .collection("users")
        .update(this.pb.authStore.record.id, {
          activeOrg: newActiveOrgId,
        });
    },
    listUserProfiles: async function (user) {
      await this.login(user);
      const userId = this.pb.authStore.record.id;
      return await this.pb.collection("profiles").getFullList({
        filter: `user = "${userId}"`,
        expand: "organization,user",
      });
    },
  };
}