-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organization identities and merging (#1458)
Co-authored-by: Gašper Grom <[email protected]>
- Loading branch information
1 parent
fe5b3f0
commit fd98789
Showing
52 changed files
with
3,903 additions
and
1,427 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import OrganizationService from '@/services/organizationService' | ||
import Permissions from '../../security/permissions' | ||
import track from '../../segment/track' | ||
import PermissionChecker from '../../services/user/permissionChecker' | ||
|
||
export default async (req, res) => { | ||
new PermissionChecker(req).validateHas(Permissions.values.organizationEdit) | ||
|
||
const payload = await new OrganizationService(req).merge( | ||
req.params.organizationId, | ||
req.body.organizationToMerge, | ||
) | ||
|
||
track('Merge organizations', { ...payload }, { ...req }) | ||
|
||
const status = payload.status || 200 | ||
|
||
await req.responseHandler.success(req, res, payload, status) | ||
} |
1 change: 1 addition & 0 deletions
1
backend/src/database/migrations/U1693899411__organizationIdentities.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 table if exists "public"."organizationIdentities"; |
128 changes: 128 additions & 0 deletions
128
backend/src/database/migrations/V1693899411__organizationIdentities.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,128 @@ | ||
create table "organizationIdentities" ( | ||
"organizationId" uuid not null references organizations on delete cascade, | ||
platform text not null, | ||
name text not null, | ||
"sourceId" text, | ||
"url" text, | ||
"tenantId" uuid not null references tenants on delete cascade, | ||
"integrationId" uuid, | ||
"createdAt" timestamp with time zone default now() not null, | ||
"updatedAt" timestamp with time zone default now() not null, | ||
primary key ("organizationId", platform, name), | ||
unique (platform, name, "tenantId") | ||
); | ||
create index "ix_organizationIdentities" on "organizationIdentities" (platform, name, "organizationId"); | ||
create index "ix_organizationIdentities_tenantId" on "organizationIdentities" ("tenantId"); | ||
create index "ix_organizationIdentities_organizationId" on "organizationIdentities" ("organizationId"); | ||
create index "organizationIdentities_createdAt_index" on "organizationIdentities" ("createdAt" desc); | ||
create index "organizationIdentities_name_index" on "organizationIdentities" (name); | ||
create trigger organization_identities_updated_at before | ||
update on "organizationIdentities" for each row execute procedure trigger_set_updated_at(); | ||
|
||
|
||
alter table organizations | ||
add "weakIdentities" jsonb default '[]'::jsonb not null; | ||
|
||
DO | ||
$$ | ||
DECLARE | ||
org organizations%ROWTYPE; | ||
act activities%ROWTYPE; | ||
gin integrations%ROWTYPE; | ||
BEGIN | ||
FOR org IN SELECT * FROM organizations | ||
WHERE NOT EXISTS (SELECT 1 FROM "organizationIdentities" WHERE "organizationId" = org.id) | ||
LOOP | ||
BEGIN | ||
-- check for organization activity on github | ||
SELECT INTO act * FROM activities WHERE "platform" = 'github' AND "organizationId" = org.id LIMIT 1; | ||
BEGIN | ||
-- If activity is found | ||
IF FOUND THEN | ||
SELECT INTO gin * | ||
FROM integrations | ||
WHERE "platform" = 'github' | ||
AND "tenantId" = org."tenantId" | ||
AND "deletedAt" IS NULL | ||
LIMIT 1; | ||
-- If integration is found | ||
IF FOUND THEN | ||
INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "url", | ||
"sourceId", "integrationId", "tenantId") | ||
VALUES (org.id, 'github', org.name, org.url, null, gin.id, org."tenantId"); | ||
-- If integration is not found, `platform` is set to 'custom' and `integrationId` is set to null | ||
ELSE | ||
INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", | ||
"integrationId", "tenantId") | ||
VALUES (org.id, 'custom', org.name, null, null, org."tenantId"); | ||
END IF; | ||
-- If no activity is found, `platform` is set to 'custom' and `integrationId` is set to null | ||
ELSE | ||
INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", | ||
"integrationId", "tenantId") | ||
VALUES (org.id, 'custom', org.name, null, null, org."tenantId"); | ||
END IF; | ||
EXCEPTION | ||
WHEN unique_violation THEN | ||
-- If conflict happens, insert this identity into organizations."weakIdentities" jsonb array | ||
UPDATE organizations | ||
SET "weakIdentities" = COALESCE("weakIdentities", '{}'::jsonb) || | ||
jsonb_build_object('platform', 'github', 'name', org.name) | ||
WHERE id = org.id; | ||
END; | ||
|
||
-- check for non-null LinkedIn handle | ||
IF org.linkedin -> 'handle' IS NOT NULL THEN | ||
BEGIN | ||
INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", | ||
"integrationId", "tenantId", "url") | ||
VALUES (org.id, 'linkedin', replace(org.linkedin ->> 'handle', 'company/', ''), null, null, | ||
org."tenantId", CONCAT('https://linkedin.com/company/', | ||
replace(org.linkedin ->> 'handle', 'company/', ''))); | ||
EXCEPTION | ||
WHEN unique_violation THEN | ||
-- If conflict happens, insert this identity into organizations."weakIdentities" jsonb array | ||
UPDATE organizations | ||
SET "weakIdentities" = COALESCE("weakIdentities", '{}'::jsonb) || | ||
jsonb_build_object('platform', 'linkedin', 'name', | ||
replace(org.linkedin ->> 'handle', 'company/', ''), | ||
'url', CONCAT('https://linkedin.com/company/', | ||
replace(org.linkedin ->> 'handle', 'company/', ''))) | ||
WHERE id = org.id; | ||
END; | ||
END IF; | ||
|
||
-- check for non-null Twitter handle | ||
IF org.twitter -> 'handle' IS NOT NULL THEN | ||
BEGIN | ||
INSERT INTO "organizationIdentities" ("organizationId", "platform", "name", "sourceId", | ||
"integrationId", "tenantId", "url") | ||
VALUES (org.id, 'twitter', org.twitter ->> 'handle'::text, null, null, org."tenantId", | ||
CONCAT('https://twitter.com/', org.twitter ->> 'handle'::text)); | ||
EXCEPTION | ||
WHEN unique_violation THEN | ||
-- If conflict happens, insert this identity into organizations."weakIdentities" jsonb array | ||
UPDATE organizations | ||
SET "weakIdentities" = COALESCE("weakIdentities", '{}'::jsonb) || | ||
jsonb_build_object('platform', 'twitter', 'name', | ||
org.twitter ->> 'handle', 'url', | ||
CONCAT('https://twitter.com/', org.twitter ->> 'handle'::text)) | ||
WHERE id = org.id; | ||
END; | ||
END IF; | ||
EXCEPTION | ||
WHEN others THEN | ||
ROLLBACK; -- Rollback the transaction when an unexpected exception occurs | ||
RAISE NOTICE 'Unexpected error for organization %: %', org.id, SQLERRM; -- Log the error | ||
END; | ||
COMMIT; | ||
END LOOP; | ||
END ; | ||
$$; | ||
|
||
|
||
drop index if exists "organizations_name_tenant_id"; | ||
|
||
alter table organizations DROP COLUMN "name"; | ||
|
||
alter table organizations DROP COLUMN "url"; |
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.