Skip to content

Commit

Permalink
feat: add author of version (#315)
Browse files Browse the repository at this point in the history
* chore: add author to pogues_version table

* feat: add author on insert version based on auth user

* chore: bump to 4.8.1-SNAPSHOT

* chore: bump version to 4.8.1-SNAPSHOT.1

* bump: version to 4.8.1
  • Loading branch information
laurentC35 authored Aug 21, 2024
1 parent 91ea58e commit f7f91a1
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<groupId>fr.insee</groupId>
<artifactId>Pogues-BO</artifactId>
<packaging>jar</packaging>
<version>4.8.0</version>
<version>4.8.1</version>
<name>Pogues-Back-Office</name>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public UserProvider getUserProvider(OidcProperties oidcProperties) {
final Jwt jwt = (Jwt) auth.getPrincipal();
return new User(
jwt.getClaimAsString(oidcProperties.stampClaim()),
jwt.getClaimAsString(oidcProperties.usernameClaim()));
jwt.getClaimAsString(oidcProperties.usernameClaim()),
jwt.getClaimAsString(oidcProperties.principalAttribute()));
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class StampsRestrictionsServiceImpl implements StampsRestrictionsService{
public User getUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User currentUser = userProvider.getUser(authentication);
log.info("Current user has stamp {}", currentUser.getStamp());
log.info("Current user ({}), has stamp {}", currentUser.getUserId(), currentUser.getStamp());
return currentUser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public class User {
private final String stamp;
@Getter
private final String name;
@Getter
private final String userId;

public User(){
this("default", "Guest");
this("default", "Guest", "guest");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public class Version {
private Timestamp timestamp;
private Date day;
private JsonNode data;
private String author;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,42 @@ public class VersionPostgresql implements QuestionnaireVersionRepository {
private static final String NOT_FOUND="Not found";
private static final String FORBIDDEN="Forbidden";


private static String COLUMNS_WITHOUT_DATA = "id, timestamp, pogues_id, day, author";
private static String COLUMNS_WITH_DATA = COLUMNS_WITHOUT_DATA + ", data";

@Override
public List<Version> getVersionsByQuestionnaireId(String poguesId, boolean withData) throws Exception {
String columns = withData ? " id, timestamp, pogues_id, day, data " : " id, timestamp, pogues_id, day ";
String columns = withData ? COLUMNS_WITH_DATA : COLUMNS_WITHOUT_DATA;
String qString =
"SELECT" + columns +
"FROM pogues_version pv WHERE pv.pogues_id = ? ORDER BY timestamp DESC;";
"SELECT " + columns +
" FROM pogues_version pv WHERE pv.pogues_id = ? ORDER BY timestamp DESC;";
return jdbcTemplate.query(qString, new VersionRowMapper(withData), poguesId);
}

@Override
public Version getLastVersionByQuestionnaireId(String poguesId, boolean withData) throws Exception {
String columns = withData ? " id, timestamp, pogues_id, day, data " : " id, timestamp, pogues_id, day ";
String columns = withData ? COLUMNS_WITH_DATA : COLUMNS_WITHOUT_DATA;
String qString =
"SELECT" + columns +
"FROM pogues_version pv WHERE pv.pogues_id = ? ORDER BY timestamp DESC LIMIT 1;";
"SELECT " + columns +
" FROM pogues_version pv WHERE pv.pogues_id = ? ORDER BY timestamp DESC LIMIT 1;";
return jdbcTemplate.queryForObject(qString, new VersionRowMapper(withData), poguesId);
}

@Override
public Version getVersionByVersionId(UUID versionId, boolean withData) throws Exception {
String columns = withData ? " id, timestamp, pogues_id, day, data " : " id, timestamp, pogues_id, day ";
String columns = withData ? COLUMNS_WITH_DATA : COLUMNS_WITHOUT_DATA;
String qString =
"SELECT" + columns +
"FROM pogues_version pv WHERE pv.id = ?;";
"SELECT " + columns +
" FROM pogues_version pv WHERE pv.id = ?;";
return jdbcTemplate.queryForObject(qString, new VersionRowMapper(withData), versionId);
}

@Override
public void createVersion(Version version) throws Exception {
String qString = """
INSERT INTO pogues_version (id, data, "timestamp", "day", pogues_id)
VALUES (?, ?, ?, ?, ?);
INSERT INTO pogues_version (id, data, "timestamp", "day", pogues_id, author)
VALUES (?, ?, ?, ?, ?, ?);
DELETE from pogues_version pv WHERE pv.id IN
(SELECT pv.id FROM pogues_version pv
Expand All @@ -80,7 +84,7 @@ SELECT day, pogues_id, MAX(timestamp)
jsonData.setValue(version.getData().toString());
jdbcTemplate.update(qString,
// insert request
version.getId(), jsonData, version.getTimestamp(), version.getDay(), version.getPoguesId(),
version.getId(), jsonData, version.getTimestamp(), version.getDay(), version.getPoguesId(), version.getAuthor(),
// Delete request: we keep last ${maxCurrentVersions} for the current day
version.getPoguesId(), version.getDay(), maxCurrentVersions,
// Delete request: we keep only the last version for each edited day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public Version mapRow(ResultSet rs, int rowNum) throws SQLException {
version.setPoguesId(rs.getString("pogues_id"));
version.setDay(rs.getDate("day"));
version.setTimestamp(rs.getTimestamp("timestamp"));
version.setAuthor(rs.getString("author"));
if(withData){
try {
version.setData(JSONFunctions.jsonStringtoJsonNode(rs.getString("data")));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.pogues.persistence.service;

import com.fasterxml.jackson.databind.JsonNode;
import fr.insee.pogues.configuration.auth.security.restrictions.StampsRestrictionsService;
import fr.insee.pogues.exception.NullReferenceException;
import fr.insee.pogues.model.Questionnaire;
import fr.insee.pogues.persistence.impl.EntityNotFoundException;
Expand Down Expand Up @@ -36,6 +37,9 @@ public class QuestionnairesServiceImpl implements QuestionnairesService {
@Autowired
private VersionService versionService;

@Autowired
protected StampsRestrictionsService stampsRestrictionsService;

public List<JsonNode> getQuestionnaireList() throws Exception {
List<JsonNode> questionnaires = questionnaireRepository.getQuestionnaires();
if (questionnaires.isEmpty()) {
Expand Down Expand Up @@ -106,7 +110,8 @@ public void createQuestionnaire(JsonNode questionnaire) throws Exception {
try {
String poguesId = questionnaire.get("id").asText();
this.questionnaireRepository.createQuestionnaire(questionnaire);
this.versionService.createVersionOfQuestionnaire(poguesId, questionnaire);
String author = stampsRestrictionsService.getUser().getUserId();
this.versionService.createVersionOfQuestionnaire(poguesId, questionnaire, author);
} catch (NonUniqueResultException e) {
throw new PoguesException(409, "Conflict", e.getMessage());
}
Expand All @@ -123,7 +128,8 @@ public void createJsonLunatic(JsonNode dataLunatic) throws Exception {
public void updateQuestionnaire(String id, JsonNode questionnaire) throws Exception {
try {
this.questionnaireRepository.updateQuestionnaire(id, questionnaire);
this.versionService.createVersionOfQuestionnaire(id, questionnaire);
String author = stampsRestrictionsService.getUser().getUserId();
this.versionService.createVersionOfQuestionnaire(id, questionnaire, author);
} catch (EntityNotFoundException e) {
throw new PoguesException(404, "Not found", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface VersionService {

Version getVersionByVersionId(UUID versionId, boolean withData) throws Exception;

void createVersionOfQuestionnaire(String poguesId, JsonNode data) throws Exception;
void createVersionOfQuestionnaire(String poguesId, JsonNode data, String author) throws Exception;

void deleteVersionsByQuestionnaireId(String poguesId) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ public Version getVersionByVersionId(UUID versionId, boolean withData) throws Ex
}

@Override
public void createVersionOfQuestionnaire(String poguesId, JsonNode data) throws Exception {
public void createVersionOfQuestionnaire(String poguesId, JsonNode data, String author) throws Exception {
Instant now = Instant.now();
Version versionToStore = new Version(
UUID.randomUUID(),
poguesId,
Timestamp.from(now),
new Date(now.toEpochMilli()),
data);
data,
author);
questionnaireVersionRepository.createVersion(versionToStore);
}

Expand All @@ -68,6 +69,6 @@ public void restoreVersion(UUID versionId) throws Exception {
// (2) Update questionnaire in pogues table
questionnaireRepository.updateQuestionnaire(version.getPoguesId(), version.getData());
// (3) Create new version
this.createVersionOfQuestionnaire(version.getPoguesId(), version.getData());
this.createVersionOfQuestionnaire(version.getPoguesId(), version.getData(), version.getAuthor());
}
}
23 changes: 23 additions & 0 deletions src/main/resources/db/changelog/200_version_questionnaire.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,27 @@
CREATE INDEX IF NOT EXISTS idx_pogues_version_pogues_id_day_timestamp_desc ON pogues_version (id, pogues_id, day, timestamp DESC);
</sql>
</changeSet>
<changeSet author="admin (generated)" id="1724250100197-4">
<dropForeignKeyConstraint baseTableName="pogues_version" constraintName="fk_pogues_pogues_version"/>
<addForeignKeyConstraint baseColumnNames="pogues_id"
baseTableName="pogues_version"
baseTableSchemaName="public"
constraintName="fk_pogues_pogues_version"
deferrable="true"
initiallyDeferred="true"
onUpdate="CASCADE"
onDelete="SET NULL"
referencedColumnNames="id"
referencedTableName="pogues"
referencedTableSchemaName="public"
validate="true"/>
</changeSet>
<changeSet author="admin (generated)" id="1724250100197-5">
<addColumn tableName="pogues_version">
<column name="author" type="VARCHAR" />
</addColumn>
<update tableName="pogues_version">
<column name="author" valueComputed="data ->> 'owner'" computed="true"/>
</update>
</changeSet>
</databaseChangeLog>

0 comments on commit f7f91a1

Please sign in to comment.