diff --git a/build/repository/review-app-repository.js b/build/repository/review-app-repository.js index e15a294e..5f1147c5 100644 --- a/build/repository/review-app-repository.js +++ b/build/repository/review-app-repository.js @@ -1,7 +1,10 @@ import { knex } from '../../db/knex-database-connection.js'; export const create = async function ({ name, repository, prNumber, parentApp }) { - await knex('review-apps').insert({ name, repository, prNumber, parentApp }); + await knex('review-apps').insert({ name, repository, prNumber, parentApp }).onConflict('name').merge({ + createdAt: new Date(), + isDeployed: false, + }); }; export const markAsDeployed = async function ({ name }) { diff --git a/test/integration/build/repository/review-app-repository_test.js b/test/integration/build/repository/review-app-repository_test.js index b2e62cb0..9cda6cd4 100644 --- a/test/integration/build/repository/review-app-repository_test.js +++ b/test/integration/build/repository/review-app-repository_test.js @@ -31,7 +31,7 @@ describe('Integration | Build | Repository | Review App', function () { }); describe('when a review app already exists', function () { - it('should throw an error', async function () { + it('should update creation date and set as not deployed', async function () { // given const name = 'pix-api-review-pr123'; const repository = 'pix'; @@ -39,12 +39,20 @@ describe('Integration | Build | Repository | Review App', function () { const parentApp = 'pix-api-review'; await reviewAppRepository.create({ name, repository, prNumber, parentApp }); + const initialReviewApp = await knex('review-apps').where({ name }).first(); // when - const error = await catchErr(reviewAppRepository.create)({ name, repository, prNumber, parentApp }); + await reviewAppRepository.create({ name, repository, prNumber, parentApp }); // then - expect(error.detail).to.equal('Key (name)=(pix-api-review-pr123) already exists.'); + const mergedReviewApp = await knex('review-apps').where({ name }).first(); + expect(mergedReviewApp).not.to.be.null; + expect(mergedReviewApp.name).to.equal(initialReviewApp.name); + expect(mergedReviewApp.repository).to.equal(initialReviewApp.repository); + expect(mergedReviewApp.prNumber).to.equal(initialReviewApp.prNumber); + expect(mergedReviewApp.parentApp).to.equal(initialReviewApp.parentApp); + expect(mergedReviewApp.isDeployed).to.be.false; + expect(mergedReviewApp.createdAt).not.to.equal(initialReviewApp.createdAt); }); }); });