GithubHelp home page GithubHelp logo

carlomigueldy / axiefy Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 4.0 816 KB

A simple scholar management app that helps owners manage their scholars.

Home Page: https://axiefy.vercel.app

License: Apache License 2.0

SCSS 0.22% Vue 69.18% JavaScript 20.67% Shell 0.15% PLpgSQL 9.66% CSS 0.12%
nuxt nuxtjs vue supabase javascript axie-infinity axie serverless aws-lambda supabasejs

axiefy's Issues

trigger bug on create new public user

Expected

When a new user is created by signing up by any method, then they are known to be as "manager" so a trigger function will attach a role to them.

While an invited user is known to be a "scholar", so we check on auth.users if the invited_at attribute is not NULL. If it is not NULL then we can confirm they are a scholar and belongs to that manager who invited them.

Details

I wrote a trigger function listening to insertions for auth.users table

-- trigger: handle new registered user
CREATE OR REPLACE FUNCTION public.handle_new_auth_user()
RETURNS TRIGGER AS $$
BEGIN 
  INSERT INTO public.users (id, email) VALUES (new.id::UUID, new.email::TEXT);

  return new;
END
$$
LANGUAGE plpgsql SECURITY DEFINER;

CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW
EXECUTE PROCEDURE public.handle_new_auth_user();

This works.

But this doesn't work and keeps returning status 500 every time a new user creates an account.

-- trigger: handle new public user created by auth.users trigger
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN 
  INSERT INTO public.teams (owner_id) VALUES (new.id::UUID); 

  -- todo: needs to get fixed
  -- scholar
  IF EXISTS(SELECT id FROM auth.users WHERE id = new.id AND invited_at IS NOT NULL) THEN
    INSERT INTO public.user_role (user_id, role_id) VALUES (
      new.id::UUID, 
      (SELECT id FROM public.roles WHERE name = 'scholar' LIMIT 1)::UUID
    );
  END IF;

  -- manager
  IF EXISTS(SELECT id FROM auth.users WHERE id = new.id AND invited_at IS NULL) THEN
    INSERT INTO public.user_role (user_id, role_id) VALUES (
      new.id::UUID, 
      (SELECT id FROM public.roles WHERE name = 'manager' LIMIT 1)::UUID
    );
    INSERT INTO public.teams (owner_id) VALUES (new.id::UUID); 
  END IF;

  return new;
END
$$
LANGUAGE plpgsql SECURITY DEFINER;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.