Skip to content

Commit

Permalink
Merge branch 'better-affiliation' into crowd-linux
Browse files Browse the repository at this point in the history
  • Loading branch information
sausage-todd committed Aug 14, 2023
2 parents 51f15a2 + 5b3775e commit 1cd5909
Show file tree
Hide file tree
Showing 65 changed files with 2,665 additions and 266 deletions.
1 change: 1 addition & 0 deletions backend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'ts-jest',
{
babelConfig: true,
isolatedModules: true,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table organizations
drop column "searchSyncedAt";
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS ix_unique_member_org_no_end_date_nulls;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE "memberSegmentAffiliations" ADD CONSTRAINT "memberSegmentAffiliations_memberId_segmentId_key" UNIQUE ("memberId", "segmentId");

ALTER TABLE "memberSegmentAffiliations" DROP COLUMN "dateStart";
ALTER TABLE "memberSegmentAffiliations" DROP COLUMN "dateEnd";

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Add the column with NOT NULL and a default value
alter table organizations
add column "searchSyncedAt" timestamptz not null default now();

-- Then, make the column nullable
alter table organizations
alter column "searchSyncedAt" drop not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- see MemberAffiliationRepository.update(), it's the same query, but without filtering by member
WITH new_activities_organizations AS (
SELECT
a.id,
(ARRAY_REMOVE(ARRAY_AGG(CASE WHEN msa.id IS NULL THEN '00000000-0000-0000-0000-000000000000' ELSE msa."organizationId" END), '00000000-0000-0000-0000-000000000000')
|| ARRAY_REMOVE(ARRAY_AGG(mo."organizationId" ORDER BY mo."dateStart" DESC), NULL)
|| ARRAY[a."organizationId"])[1] AS new_org
FROM activities a
LEFT JOIN "memberSegmentAffiliations" msa ON msa."memberId" = a."memberId" AND a."segmentId" = msa."segmentId"
LEFT JOIN "memberOrganizations" mo ON mo."memberId" = a."memberId" AND (
a.timestamp BETWEEN mo."dateStart" AND mo."dateEnd"
OR (a.timestamp >= mo."dateStart" AND mo."dateEnd" IS NULL)
)
GROUP BY a.id
)
UPDATE activities a1
SET "organizationId" = nao.new_org
FROM new_activities_organizations nao
WHERE a1.id = nao.id
AND ("organizationId" != nao.new_org
OR ("organizationId" IS NULL AND nao.new_org IS NOT NULL)
OR ("organizationId" IS NOT NULL AND nao.new_org IS NULL));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE UNIQUE INDEX IF NOT EXISTS ix_unique_member_org_no_end_date_nulls
ON "memberOrganizations" ("memberId", "organizationId", "dateStart")
WHERE "dateEnd" is NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE "memberSegmentAffiliations" ADD COLUMN "dateStart" TIMESTAMP WITH TIME ZONE NULL;
ALTER TABLE "memberSegmentAffiliations" ADD COLUMN "dateEnd" TIMESTAMP WITH TIME ZONE NULL;

ALTER TABLE "memberSegmentAffiliations" DROP CONSTRAINT "memberSegmentAffiliations_memberId_segmentId_key";
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
DO $$
DECLARE
_work_experience RECORD;
_org_id UUID;
_org_name TEXT;
_date_start TIMESTAMP WITHOUT TIME ZONE;
_date_end TIMESTAMP WITHOUT TIME ZONE;
BEGIN
CREATE OR REPLACE FUNCTION __convert_iso_string_to_timestamp(iso_string text) RETURNS timestamp AS $inner$
BEGIN
IF iso_string = 'Present' THEN
RETURN NULL;
ELSIF (length(iso_string) = 4) THEN -- Only year
RETURN to_timestamp(iso_string, 'YYYY');
ELSIF (length(iso_string) = 7) THEN -- Year and month
RETURN to_timestamp(iso_string, 'YYYY-MM');
ELSE -- Full timestamp
RETURN iso_string::TIMESTAMP WITH TIME ZONE;
END IF;
END;
$inner$ LANGUAGE plpgsql;


FOR _work_experience IN SELECT
m.id,
m."tenantId",
w.value->>'title' AS title,
w.value->>'company' AS company,
CASE WHEN w.value->>'endDate' != 'Present' THEN w.value->>'endDate' END AS "dateEnd",
w.value->>'startDate' AS "dateStart"
FROM members m, LATERAL (SELECT value FROM jsonb_array_elements(m.attributes->'workExperiences'->'default')) as w
WHERE m.attributes->'workExperiences' IS NOT NULL
AND w.value->'startDate' IS NOT NULL
LOOP
_org_name := _work_experience.company;
IF _org_name IS NULL THEN
CONTINUE;
END IF;

_date_start = __convert_iso_string_to_timestamp(_work_experience."dateStart");
_date_end = __convert_iso_string_to_timestamp(_work_experience."dateEnd");

SELECT id INTO _org_id FROM organizations WHERE name = _org_name AND "tenantId" = _work_experience."tenantId";
IF _org_id IS NULL THEN
_org_id := uuid_generate_v4();

INSERT INTO organizations (id, "updatedAt", "createdAt", "tenantId", name, "displayName")
VALUES (_org_id, NOW(), NOW(), _work_experience."tenantId", _org_name, _org_name);
RAISE NOTICE 'Created organization % id=%', _org_name, _org_id;
ELSE
RAISE NOTICE 'Found organization % id=%', _org_name, _org_id;
END IF;

DELETE
FROM "memberOrganizations"
WHERE "memberId" = _work_experience.id
AND "organizationId" = _org_id
AND "dateStart" IS NULL
AND "dateEnd" IS NULL;

IF _date_end IS NULL THEN
INSERT INTO "memberOrganizations" ("createdAt", "updatedAt", "memberId", "organizationId", "dateStart", "dateEnd", title)
VALUES (NOW(), NOW(), _work_experience.id, _org_id, _date_start, _date_end, _work_experience.title)
ON CONFLICT ("memberId", "organizationId", "dateStart") WHERE ("dateEnd" IS NULL) DO NOTHING;
ELSE
INSERT INTO "memberOrganizations" ("createdAt", "updatedAt", "memberId", "organizationId", "dateStart", "dateEnd", title)
VALUES (NOW(), NOW(), _work_experience.id, _org_id, _date_start, _date_end, _work_experience.title)
ON CONFLICT ("memberId", "organizationId", "dateStart", "dateEnd") DO NOTHING;
END IF;

END LOOP;

DROP FUNCTION IF EXISTS __convert_iso_string_to_timestamp(text);
END;
$$
4 changes: 4 additions & 0 deletions backend/src/database/models/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export default (sequelize) => {
len: [0, 255],
},
},
organizationId: {
type: DataTypes.UUID,
allowNull: true,
},
},
{
indexes: [
Expand Down
2 changes: 1 addition & 1 deletion backend/src/database/models/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default (sequelize) => {
type: DataTypes.TEXT,
},
slug: {
type: DataTypes.DATE,
type: DataTypes.TEXT,
allowNull: false,
},
parentSlug: {
Expand Down
Loading

0 comments on commit 1cd5909

Please sign in to comment.