summaryrefslogtreecommitdiff
path: root/db/migrations/0193_fast_zodiak.sql
blob: 982a2ca640efa48e258dcc7ec202258106df11b2 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
DROP VIEW "public"."tech_vendor_detail_view";--> statement-breakpoint
DROP VIEW "public"."tech_vendor_items_view";--> statement-breakpoint
CREATE VIEW "public"."tech_vendor_detail_view" AS (select "id", "vendor_name", "vendor_code", "tax_id", "address", "country", "country_eng", "country_fab", "agent_name", "agent_phone", "agent_email", "phone", "email", "website", "status", "tech_vendor_type", "representative_name", "representative_email", "representative_phone", "representative_birth", "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 = tech_vendors.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 tech_vendor_attachments a
      WHERE a.vendor_id = tech_vendors.id)
     as "attachments", 
      (SELECT COUNT(*)
      FROM tech_vendor_attachments a
      WHERE a.vendor_id = tech_vendors.id)
     as "attachment_count", 
        (SELECT COUNT(*) 
        FROM vendor_contacts c
        WHERE c.vendor_id = tech_vendors.id)
       as "contact_count", 
        (SELECT COALESCE(
          json_agg(
            json_build_object(
              'itemCode', i.item_code,
            )
          ),
          '[]'::json
        )
        FROM tech_vendor_possible_items i
        LEFT JOIN items it ON i.item_code = it.item_code
        WHERE i.vendor_id = tech_vendors.id)
       as "possible_items", 
        (SELECT COUNT(*) 
        FROM tech_vendor_possible_items i
        WHERE i.vendor_id = tech_vendors.id)
       as "item_count" from "tech_vendors");--> statement-breakpoint
CREATE VIEW "public"."tech_vendor_items_view" AS (select "tech_vendor_possible_items"."id", "tech_vendor_possible_items"."vendor_id", "items"."item_code", "tech_vendor_possible_items"."created_at", "tech_vendor_possible_items"."updated_at" from "tech_vendor_possible_items" left join "items" on "tech_vendor_possible_items"."item_code" = "items"."item_code");