Skip to content

Commit

Permalink
[TECH] Ajouter une contrainte sur la table last-user-application-conn…
Browse files Browse the repository at this point in the history
…ections assurant l'unicité du couple userId-application (PIX-16769)

 #11544
  • Loading branch information
pix-service-auto-merge authored Mar 4, 2025
2 parents e3495c1 + 1eee8a1 commit e011ba4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const TABLE_NAME = 'last-user-application-connections';

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
const up = async function (knex) {
return knex.schema.table(TABLE_NAME, (table) => {
table.unique(['userId', 'application']);
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
const down = async function (knex) {
return knex.schema.table(TABLE_NAME, (table) => {
table.dropUnique(['userId', 'application']);
});
};

export { down, up };
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { knex } from '../../../../db/knex-database-connection.js';
const TABLE_NAME = 'last-user-application-connections';

async function upsert({ userId, application, lastLoggedAt }) {
const existingConnection = await knex(TABLE_NAME).where({ userId, application }).first();

if (existingConnection) {
return knex(TABLE_NAME).where({ userId, application }).update({ lastLoggedAt });
}

return knex(TABLE_NAME).insert({ userId, application, lastLoggedAt });
return knex(TABLE_NAME)
.insert({ userId, application, lastLoggedAt })
.onConflict(['userId', 'application'])
.merge({ lastLoggedAt });
}

export const lastUserApplicationConnectionsRepository = { upsert };

0 comments on commit e011ba4

Please sign in to comment.