-- NOTE: This file is auto generated by ./sql-generator

-- UserRepository.get
select
  "id",
  "name",
  "email",
  "avatarColor",
  "profileImagePath",
  "profileChangedAt",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "isAdmin",
  "status",
  "oauthId",
  "profileImagePath",
  "shouldChangePassword",
  "storageLabel",
  "quotaSizeInBytes",
  "quotaUsageInBytes",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "user_metadata"."key",
          "user_metadata"."value"
        from
          "user_metadata"
        where
          "user"."id" = "user_metadata"."userId"
      ) as agg
  ) as "metadata"
from
  "user"
where
  "user"."id" = $1
  and "user"."deletedAt" is null

-- UserRepository.getAdmin
select
  "id",
  "name",
  "email",
  "avatarColor",
  "profileImagePath",
  "profileChangedAt",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "isAdmin",
  "status",
  "oauthId",
  "profileImagePath",
  "shouldChangePassword",
  "storageLabel",
  "quotaSizeInBytes",
  "quotaUsageInBytes",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "user_metadata"."key",
          "user_metadata"."value"
        from
          "user_metadata"
        where
          "user"."id" = "user_metadata"."userId"
      ) as agg
  ) as "metadata"
from
  "user"
where
  "user"."isAdmin" = $1
  and "user"."deletedAt" is null

-- UserRepository.getFileSamples
select
  "id",
  "profileImagePath"
from
  "user"
where
  "profileImagePath" != ''
limit
  3

-- UserRepository.hasAdmin
select
  "user"."id"
from
  "user"
where
  "user"."isAdmin" = $1
  and "user"."deletedAt" is null

-- UserRepository.getForPinCode
select
  "user"."pinCode",
  "user"."password"
from
  "user"
where
  "user"."id" = $1
  and "user"."deletedAt" is null

-- UserRepository.getForChangePassword
select
  "user"."id",
  "user"."password"
from
  "user"
where
  "user"."id" = $1
  and "user"."deletedAt" is null

-- UserRepository.getByEmail
select
  "id",
  "name",
  "email",
  "avatarColor",
  "profileImagePath",
  "profileChangedAt",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "isAdmin",
  "status",
  "oauthId",
  "profileImagePath",
  "shouldChangePassword",
  "storageLabel",
  "quotaSizeInBytes",
  "quotaUsageInBytes",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "user_metadata"."key",
          "user_metadata"."value"
        from
          "user_metadata"
        where
          "user"."id" = "user_metadata"."userId"
      ) as agg
  ) as "metadata"
from
  "user"
where
  "email" = $1
  and "user"."deletedAt" is null

-- UserRepository.getByStorageLabel
select
  "id",
  "name",
  "email",
  "avatarColor",
  "profileImagePath",
  "profileChangedAt",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "isAdmin",
  "status",
  "oauthId",
  "profileImagePath",
  "shouldChangePassword",
  "storageLabel",
  "quotaSizeInBytes",
  "quotaUsageInBytes"
from
  "user"
where
  "user"."storageLabel" = $1
  and "user"."deletedAt" is null

-- UserRepository.getByOAuthId
select
  "id",
  "name",
  "email",
  "avatarColor",
  "profileImagePath",
  "profileChangedAt",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "isAdmin",
  "status",
  "oauthId",
  "profileImagePath",
  "shouldChangePassword",
  "storageLabel",
  "quotaSizeInBytes",
  "quotaUsageInBytes",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "user_metadata"."key",
          "user_metadata"."value"
        from
          "user_metadata"
        where
          "user"."id" = "user_metadata"."userId"
      ) as agg
  ) as "metadata"
from
  "user"
where
  "user"."oauthId" = $1
  and "user"."deletedAt" is null

-- UserRepository.getDeletedAfter
select
  "id"
from
  "user"
where
  "user"."deletedAt" < $1

-- UserRepository.getList (with deleted)
select
  "id",
  "name",
  "email",
  "avatarColor",
  "profileImagePath",
  "profileChangedAt",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "isAdmin",
  "status",
  "oauthId",
  "profileImagePath",
  "shouldChangePassword",
  "storageLabel",
  "quotaSizeInBytes",
  "quotaUsageInBytes",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "user_metadata"."key",
          "user_metadata"."value"
        from
          "user_metadata"
        where
          "user"."id" = "user_metadata"."userId"
      ) as agg
  ) as "metadata"
from
  "user"
order by
  "createdAt" desc

-- UserRepository.getList (without deleted)
select
  "id",
  "name",
  "email",
  "avatarColor",
  "profileImagePath",
  "profileChangedAt",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "isAdmin",
  "status",
  "oauthId",
  "profileImagePath",
  "shouldChangePassword",
  "storageLabel",
  "quotaSizeInBytes",
  "quotaUsageInBytes",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "user_metadata"."key",
          "user_metadata"."value"
        from
          "user_metadata"
        where
          "user"."id" = "user_metadata"."userId"
      ) as agg
  ) as "metadata"
from
  "user"
where
  "user"."deletedAt" is null
order by
  "createdAt" desc

-- UserRepository.getUserStats
select
  "user"."id" as "userId",
  "user"."name" as "userName",
  "user"."quotaSizeInBytes",
  count(*) filter (
    where
      (
        "asset"."type" = 'IMAGE'
        and "asset"."visibility" != 'hidden'
      )
  ) as "photos",
  count(*) filter (
    where
      (
        "asset"."type" = 'VIDEO'
        and "asset"."visibility" != 'hidden'
      )
  ) as "videos",
  coalesce(
    sum("asset_exif"."fileSizeInByte") filter (
      where
        "asset"."libraryId" is null
    ),
    0
  ) as "usage",
  coalesce(
    sum("asset_exif"."fileSizeInByte") filter (
      where
        (
          "asset"."libraryId" is null
          and "asset"."type" = 'IMAGE'
        )
    ),
    0
  ) as "usagePhotos",
  coalesce(
    sum("asset_exif"."fileSizeInByte") filter (
      where
        (
          "asset"."libraryId" is null
          and "asset"."type" = 'VIDEO'
        )
    ),
    0
  ) as "usageVideos"
from
  "user"
  left join "asset" on "asset"."ownerId" = "user"."id"
  and "asset"."deletedAt" is null
  left join "asset_exif" on "asset_exif"."assetId" = "asset"."id"
group by
  "user"."id"
order by
  "user"."createdAt" asc

-- UserRepository.getCount
select
  count(*) as "count"
from
  "user"
where
  "user"."deletedAt" is null

-- UserRepository.updateUsage
update "user"
set
  "quotaUsageInBytes" = "quotaUsageInBytes" + $1,
  "updatedAt" = $2
where
  "id" = $3::uuid
  and "user"."deletedAt" is null

-- UserRepository.syncUsage
update "user"
set
  "quotaUsageInBytes" = (
    select
      coalesce(sum("asset_exif"."fileSizeInByte"), 0) as "usage"
    from
      "asset"
      left join "asset_exif" on "asset_exif"."assetId" = "asset"."id"
    where
      "asset"."libraryId" is null
      and "asset"."ownerId" = "user"."id"
  ),
  "updatedAt" = $1
where
  "user"."deletedAt" is null
  and "user"."id" = $2::uuid
