Multiple groups per contact, legacy group text migrated, excel import/export, contacts filter and column, richer create-in-tahesab, match-first bulk export
86 lines
4.2 KiB
SQL
86 lines
4.2 KiB
SQL
-- CreateTable
|
|
CREATE TABLE "CustomerGroup" (
|
|
"id" TEXT NOT NULL,
|
|
"organizationId" TEXT NOT NULL,
|
|
"name" TEXT NOT NULL,
|
|
"color" TEXT NOT NULL DEFAULT 'teal',
|
|
"position" INTEGER NOT NULL DEFAULT 0,
|
|
"tahesabConnectionId" TEXT,
|
|
"tahesabGid" TEXT,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
|
|
CONSTRAINT "CustomerGroup_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "PersonGroupLink" (
|
|
"personId" TEXT NOT NULL,
|
|
"groupId" TEXT NOT NULL,
|
|
"source" TEXT NOT NULL DEFAULT 'manual',
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "PersonGroupLink_pkey" PRIMARY KEY ("personId","groupId")
|
|
);
|
|
|
|
-- CreateTable
|
|
CREATE TABLE "CompanyGroupLink" (
|
|
"companyId" TEXT NOT NULL,
|
|
"groupId" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT "CompanyGroupLink_pkey" PRIMARY KEY ("companyId","groupId")
|
|
);
|
|
|
|
-- CreateIndex
|
|
CREATE UNIQUE INDEX "CustomerGroup_organizationId_name_key" ON "CustomerGroup"("organizationId", "name");
|
|
CREATE INDEX "CustomerGroup_organizationId_position_idx" ON "CustomerGroup"("organizationId", "position");
|
|
CREATE INDEX "CustomerGroup_tahesabConnectionId_tahesabGid_idx" ON "CustomerGroup"("tahesabConnectionId", "tahesabGid");
|
|
CREATE INDEX "PersonGroupLink_groupId_idx" ON "PersonGroupLink"("groupId");
|
|
CREATE INDEX "CompanyGroupLink_groupId_idx" ON "CompanyGroupLink"("groupId");
|
|
|
|
-- AddForeignKey
|
|
ALTER TABLE "CustomerGroup" ADD CONSTRAINT "CustomerGroup_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
ALTER TABLE "PersonGroupLink" ADD CONSTRAINT "PersonGroupLink_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
ALTER TABLE "PersonGroupLink" ADD CONSTRAINT "PersonGroupLink_groupId_fkey" FOREIGN KEY ("groupId") REFERENCES "CustomerGroup"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
ALTER TABLE "CompanyGroupLink" ADD CONSTRAINT "CompanyGroupLink_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
ALTER TABLE "CompanyGroupLink" ADD CONSTRAINT "CompanyGroupLink_groupId_fkey" FOREIGN KEY ("groupId") REFERENCES "CustomerGroup"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- تبدیل دادههای قبلی: «گروه مشتری» اکسل (customFields.group) و «گروهبندی شرکت» (groupName).
|
|
-- مقدارهای چندتایی با «،» «,» «/» جدا میشوند. برچسبهای «وضعیت مشتری» که دیدار در همان ستون میگذارد گروه حساب نمیشوند.
|
|
CREATE TEMP TABLE "_cg_src" AS
|
|
SELECT p."organizationId" AS org, 'person' AS kind, p."id" AS ref, left(btrim(x), 80) AS name
|
|
FROM "Person" p
|
|
CROSS JOIN LATERAL regexp_split_to_table(p."customFields"->>'group', '\s*[،,/]\s*') AS x
|
|
WHERE COALESCE(p."customFields"->>'group', '') <> ''
|
|
UNION ALL
|
|
SELECT c."organizationId", 'company', c."id", left(btrim(x), 80)
|
|
FROM "Company" c
|
|
CROSS JOIN LATERAL regexp_split_to_table(c."groupName", '\s*[،,/]\s*') AS x
|
|
WHERE COALESCE(c."groupName", '') <> '';
|
|
|
|
DELETE FROM "_cg_src"
|
|
WHERE name = ''
|
|
OR name IN ('مشتری تازه وارد', 'مشتری خوشحال', 'مشتری ناراضی', 'مشتری خاص');
|
|
|
|
INSERT INTO "CustomerGroup" ("id", "organizationId", "name", "color", "position", "createdAt", "updatedAt")
|
|
SELECT 'cgrp_' || md5(d.org || '|' || d.name), d.org, d.name, 'teal', 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
|
|
FROM (SELECT DISTINCT org, name FROM "_cg_src") AS d
|
|
ON CONFLICT ("organizationId", "name") DO NOTHING;
|
|
|
|
INSERT INTO "PersonGroupLink" ("personId", "groupId", "source", "createdAt")
|
|
SELECT DISTINCT s.ref, g."id", 'manual', CURRENT_TIMESTAMP
|
|
FROM "_cg_src" s
|
|
JOIN "CustomerGroup" g ON g."organizationId" = s.org AND g."name" = s.name
|
|
WHERE s.kind = 'person'
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
INSERT INTO "CompanyGroupLink" ("companyId", "groupId", "createdAt")
|
|
SELECT DISTINCT s.ref, g."id", CURRENT_TIMESTAMP
|
|
FROM "_cg_src" s
|
|
JOIN "CustomerGroup" g ON g."organizationId" = s.org AND g."name" = s.name
|
|
WHERE s.kind = 'company'
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
DROP TABLE "_cg_src";
|