<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userOverride">

    <select id="getUserOverride" parameterType="map" resultType="map">
        SELECT OVERRIDE_ID
            , USER_ID
            , CARD_ID
            , OVERRIDES
            , CREATED_DATE
            , UPDATED_DATE
        FROM USER_OVERRIDES
        WHERE USER_ID = #{user_id}
        AND CARD_ID = #{card_id}
    </select>

    <insert id="upsertUserOverride" parameterType="map">
        INSERT INTO USER_OVERRIDES (
            OVERRIDE_ID
            , USER_ID
            , CARD_ID
            , OVERRIDES
            , CREATED_DATE
            , UPDATED_DATE
        ) VALUES (
            #{override_id}
            , #{user_id}
            , #{card_id}
            , #{overrides}::jsonb
            , CURRENT_TIMESTAMP
            , CURRENT_TIMESTAMP
        )
        ON CONFLICT (USER_ID, CARD_ID)
        DO UPDATE SET
            OVERRIDES = #{overrides}::jsonb
            , UPDATED_DATE = CURRENT_TIMESTAMP
    </insert>

    <delete id="deleteUserOverride" parameterType="map">
        DELETE FROM USER_OVERRIDES
        WHERE USER_ID = #{user_id}
        AND CARD_ID = #{card_id}
    </delete>

</mapper>
