summaryrefslogtreecommitdiff
path: root/db/migrations_backup/0106_mature_hawkeye.sql
blob: 7d6fde8d5bd72aaa3ab8fe3758512da6bb1a2a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
DROP TABLE "tasks" CASCADE;--> statement-breakpoint
CREATE VIEW "public"."vendor_detail_view" AS (select "id", "vendor_name", "vendor_code", "tax_id", "address", "country", "phone", "email", "website", "status", "representative_name", "representative_birth", "representative_email", "representative_phone", "corporate_registration_number", "credit_agency", "credit_rating", "cash_flow_rating", "created_at", "updated_at", (
        SELECT COALESCE(
          json_agg(
            json_build_object(
              'id', c.id,
              'contactName', c.contact_name,
              'contactPosition', c.contact_position,
              'contactEmail', c.contact_email,
              'contactPhone', c.contact_phone,
              'isPrimary', c.is_primary
            )
          ),
          '[]'::json
        )
        FROM vendor_contacts c
        WHERE c.vendor_id = "id"
      ) as "contacts", (
        SELECT COALESCE(
          json_agg(
            json_build_object(
              'id', a.id,
              'fileName', a.file_name,
              'filePath', a.file_path,
              'attachmentType', a.attachment_type,
              'createdAt', a.created_at
            )
            ORDER BY a.attachment_type, a.created_at DESC
          ),
          '[]'::json
        )
        FROM vendor_attachments a
        WHERE a.vendor_id = "id"
      ) as "attachments", (
        SELECT COUNT(*)
        FROM vendor_attachments a
        WHERE a.vendor_id = "id"
      ) as "attachment_count", (
        SELECT COUNT(*) 
        FROM vendor_contacts c
        WHERE c.vendor_id = "id"
      ) as "contact_count" from "vendors");