-
Notifications
You must be signed in to change notification settings - Fork 752
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'better-affiliation' into crowd-linux
- Loading branch information
Showing
65 changed files
with
2,665 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ module.exports = { | |
'ts-jest', | ||
{ | ||
babelConfig: true, | ||
isolatedModules: true, | ||
}, | ||
], | ||
}, | ||
|
2 changes: 2 additions & 0 deletions
2
backend/src/database/migrations/U1691485202__organization-sync-timestamp.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alter table organizations | ||
drop column "searchSyncedAt"; |
Empty file.
1 change: 1 addition & 0 deletions
1
backend/src/database/migrations/U1691569430__org_members_3rd_unique_index.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
5 changes: 5 additions & 0 deletions
5
backend/src/database/migrations/U1691658076__segment-affiliation-dates.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
7 changes: 7 additions & 0 deletions
7
backend/src/database/migrations/V1691485202__organization-sync-timestamp.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
22 changes: 22 additions & 0 deletions
22
backend/src/database/migrations/V1691493013__past-activity-affiliation.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
3 changes: 3 additions & 0 deletions
3
backend/src/database/migrations/V1691569430__org_members_3rd_unique_index.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
4 changes: 4 additions & 0 deletions
4
backend/src/database/migrations/V1691658076__segment-affiliation-dates.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
75 changes: 75 additions & 0 deletions
75
backend/src/database/migrations/V1691667297__restore-work-experience.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
$$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.