Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IPCT1-486] - replace community state #147

Merged
merged 12 commits into from
Dec 7, 2021
71 changes: 71 additions & 0 deletions src/database/migrations/z1636472208-update-triggers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

// eslint-disable-next-line no-undef
module.exports = {
async up(queryInterface, Sequelize) {
if (process.env.NODE_ENV === 'test') {
return;
}

await queryInterface.sequelize.query(`
CREATE OR REPLACE FUNCTION update_inflow_community_states()
RETURNS TRIGGER AS $$
declare
-- state_raised numeric(29);
-- state_daily_raised numeric(29);
n_backer bigint;
community_id integer;
BEGIN
SELECT id INTO community_id FROM community where "contractAddress"=NEW."contractAddress";

IF community_id is null THEN
return new;
end if;
-- if this address never donated, it's a new backer
SELECT count(*) INTO n_backer FROM inflow WHERE "from" = NEW."from" AND "contractAddress"=NEW."contractAddress";
IF n_backer = 0 THEN
UPDATE ubi_community_state SET backers = backers + 1 WHERE "communityId"=community_id;
end if;
-- update total raised
-- SELECT SUM(raised + NEW.amount) INTO state_raised FROM ubi_community_state WHERE "communityId"=community_id;
-- UPDATE ubi_community_state SET raised = state_raised WHERE "communityId"=community_id;
-- SELECT SUM(raised + NEW.amount) INTO state_daily_raised FROM ubi_community_daily_state WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
-- UPDATE ubi_community_daily_state SET raised = state_daily_raised WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed unused lines on this file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return NEW;
END;
$$ LANGUAGE plpgsql;`);

await queryInterface.sequelize.query(`
CREATE OR REPLACE FUNCTION update_claim_states()
RETURNS TRIGGER AS $$
declare
-- state_claimed numeric(29);
-- state_daily_claimed numeric(29);
beneficiary_claimed numeric(22);
beneficiary_last_claim_at timestamp with time zone;
community_public_id uuid;
BEGIN
SELECT "publicId" INTO community_public_id FROM community where id=NEW."communityId";
-- update claims
UPDATE ubi_community_state SET claims = claims + 1 WHERE "communityId"=NEW."communityId";
-- UPDATE ubi_community_daily_state SET claims = claims + 1 WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
-- update beneficiary table as well
SELECT "lastClaimAt" INTO beneficiary_last_claim_at FROM beneficiary WHERE "communityId"=community_public_id AND address=NEW.address;
UPDATE beneficiary SET claims = claims + 1, "penultimateClaimAt"=beneficiary_last_claim_at, "lastClaimAt"=NEW."txAt" WHERE "communityId"=community_public_id AND address=NEW.address;
SELECT SUM(claimed + NEW.amount) INTO beneficiary_claimed FROM beneficiary WHERE "communityId"=community_public_id AND address=NEW.address;
UPDATE beneficiary SET claimed = beneficiary_claimed WHERE "communityId"=community_public_id AND address=NEW.address;
-- update total claimed
-- SELECT SUM(claimed + NEW.amount) INTO state_claimed FROM ubi_community_state WHERE "communityId"=NEW."communityId";
-- UPDATE ubi_community_state SET claimed = state_claimed WHERE "communityId"=NEW."communityId";
-- SELECT SUM(claimed + NEW.amount) INTO state_daily_claimed FROM ubi_community_daily_state WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
-- UPDATE ubi_community_daily_state SET claimed = state_daily_claimed WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
return NEW;
END;
$$ LANGUAGE plpgsql;`);

await queryInterface.sequelize.query(`DROP TRIGGER IF EXISTS update_managers_community_state ON manager`);
await queryInterface.sequelize.query(`DROP TRIGGER IF EXISTS update_beneficiaries_community_states ON beneficiary`);
},

down(queryInterface, Sequelize) {},
};
75 changes: 1 addition & 74 deletions src/database/migrations/zz-create-triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,35 @@ module.exports = {
up: async (queryInterface, Sequelize) => {
// use datagrip for better understanding + highlight

// create trigger to update total beneficiaries ina given community, by day and since ever
await queryInterface.sequelize.query(`
CREATE OR REPLACE FUNCTION update_beneficiaries_community_states()
RETURNS TRIGGER AS $$
declare
community_id integer;
BEGIN
SELECT id INTO community_id FROM community where "publicId"=NEW."communityId";
IF (TG_OP = 'INSERT') THEN -- INSERT operations (beneficiary being added from community)
-- update overall state
UPDATE ubi_community_state SET beneficiaries = beneficiaries + 1 WHERE "communityId"=community_id;
-- update daily state
-- UPDATE ubi_community_daily_state SET beneficiaries = beneficiaries + 1 WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
ELSEIF (OLD.active IS TRUE AND NEW.active IS FALSE) THEN -- beneficiary being removed from community
-- update overall state
UPDATE ubi_community_state SET beneficiaries = beneficiaries - 1, "removedBeneficiaries" = "removedBeneficiaries" + 1 WHERE "communityId"=community_id;
-- update daily state
-- UPDATE ubi_community_daily_state SET beneficiaries = beneficiaries - 1 WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_beneficiaries_community_states
BEFORE INSERT OR UPDATE
ON beneficiary
FOR EACH ROW
EXECUTE PROCEDURE update_beneficiaries_community_states();`);

await queryInterface.sequelize.query(`
CREATE OR REPLACE FUNCTION update_claim_states()
RETURNS TRIGGER AS $$
declare
state_claimed numeric(29);
-- state_daily_claimed numeric(29);
beneficiary_claimed numeric(22);
beneficiary_last_claim_at timestamp with time zone;
community_public_id uuid;
BEGIN
SELECT "publicId" INTO community_public_id FROM community where id=NEW."communityId";
-- update claims
UPDATE ubi_community_state SET claims = claims + 1 WHERE "communityId"=NEW."communityId";
-- UPDATE ubi_community_daily_state SET claims = claims + 1 WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
-- update beneficiary table as well
SELECT "lastClaimAt" INTO beneficiary_last_claim_at FROM beneficiary WHERE "communityId"=community_public_id AND address=NEW.address;
UPDATE beneficiary SET claims = claims + 1, "penultimateClaimAt"=beneficiary_last_claim_at, "lastClaimAt"=NEW."txAt" WHERE "communityId"=community_public_id AND address=NEW.address;
SELECT SUM(claimed + NEW.amount) INTO beneficiary_claimed FROM beneficiary WHERE "communityId"=community_public_id AND address=NEW.address;
UPDATE beneficiary SET claimed = beneficiary_claimed WHERE "communityId"=community_public_id AND address=NEW.address;
-- update total claimed
SELECT SUM(claimed + NEW.amount) INTO state_claimed FROM ubi_community_state WHERE "communityId"=NEW."communityId";
UPDATE ubi_community_state SET claimed = state_claimed WHERE "communityId"=NEW."communityId";
-- SELECT SUM(claimed + NEW.amount) INTO state_daily_claimed FROM ubi_community_daily_state WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
-- UPDATE ubi_community_daily_state SET claimed = state_daily_claimed WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
return NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_claim_states
BEFORE INSERT
ON ubi_claim
FOR EACH ROW
EXECUTE PROCEDURE update_claim_states();`);

await queryInterface.sequelize.query(`
CREATE OR REPLACE FUNCTION update_managers_community_state()
RETURNS TRIGGER AS
$$
declare
community_id integer;
BEGIN
SELECT id INTO community_id FROM community where "publicId" = NEW."communityId";
IF (TG_OP = 'INSERT') THEN -- INSERT operations
-- update overall state
UPDATE ubi_community_state SET managers = managers + 1 WHERE "communityId" = community_id;
ELSEIF (OLD.active IS TRUE AND NEW.active IS FALSE) THEN -- manager being removed from community
-- update overall state
UPDATE ubi_community_state SET managers = managers - 1 WHERE "communityId" = community_id;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_managers_community_state
BEFORE INSERT OR UPDATE
ON manager
FOR EACH ROW
EXECUTE PROCEDURE update_managers_community_state();`);

await queryInterface.sequelize.query(`
CREATE OR REPLACE FUNCTION update_inflow_community_states()
RETURNS TRIGGER AS $$
declare
state_raised numeric(29);
-- state_daily_raised numeric(29);
n_backer bigint;
community_id integer;
BEGIN
Expand All @@ -107,21 +42,14 @@ EXECUTE PROCEDURE update_managers_community_state();`);
IF community_id is null THEN
return new;
end if;

-- if this address never donated, it's a new backer
SELECT count(*) INTO n_backer FROM inflow WHERE "from" = NEW."from" AND "contractAddress"=NEW."contractAddress";
IF n_backer = 0 THEN
UPDATE ubi_community_state SET backers = backers + 1 WHERE "communityId"=community_id;
end if;
-- update total raised
SELECT SUM(raised + NEW.amount) INTO state_raised FROM ubi_community_state WHERE "communityId"=community_id;
UPDATE ubi_community_state SET raised = state_raised WHERE "communityId"=community_id;
-- SELECT SUM(raised + NEW.amount) INTO state_daily_raised FROM ubi_community_daily_state WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
-- UPDATE ubi_community_daily_state SET raised = state_daily_raised WHERE "communityId"=community_id AND date=DATE(NEW."txAt");
return NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_inflow_community_states
BEFORE INSERT
ON inflow
Expand All @@ -141,7 +69,6 @@ EXECUTE PROCEDURE update_inflow_community_states();`);
END IF;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_loves_stories
BEFORE INSERT OR DELETE
ON story_user_engagement
Expand All @@ -150,4 +77,4 @@ EXECUTE PROCEDURE update_loves_stories();`);
},

down(queryInterface, Sequelize) {},
};
};
12 changes: 12 additions & 0 deletions src/database/models/associations/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export function communityAssociation(sequelize: Sequelize) {
as: 'beneficiaries',
});

// used to query from the community with incude
sequelize.models.Community.hasMany(sequelize.models.Manager, {
foreignKey: 'communityId',
sourceKey: 'publicId',
as: 'managers',
});

// used to query from the promoter with incude
sequelize.models.UbiPromoterModel.belongsToMany(
sequelize.models.Community,
Expand Down Expand Up @@ -102,4 +109,9 @@ export function communityAssociation(sequelize: Sequelize) {
sourceKey: 'proposalId',
as: 'proposal',
});
sequelize.models.Community.hasMany(sequelize.models.UbiClaimModel, {
foreignKey: 'communityId',
sourceKey: 'id',
as: 'claims',
});
}
12 changes: 6 additions & 6 deletions src/interfaces/ubi/ubiCommunityState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@
* description: Number of unique backers since contract inception
*/
export interface UbiCommunityState {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove all instances of the community state table

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but do not add a drop table yet (needs to be at a later stage)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!
I kept only the interface

communityId: number;
communityId?: number;
claimed: string;
claims: number;
claims?: number;
beneficiaries: number; // only in community
removedBeneficiaries: number;
managers: number;
removedBeneficiaries?: number;
managers?: number;
raised: string;
backers: number;

// timestamps
createdAt: Date;
updatedAt: Date;
createdAt?: Date;
updatedAt?: Date;
}

export interface UbiCommunityStateCreation {
Expand Down
Loading