Skip to content

Commit

Permalink
[TEAMMATES#12048] Add is migrated flag to datastore account (TEAMMATE…
Browse files Browse the repository at this point in the history
  • Loading branch information
daongochieu2810 authored and hhdqirui committed Feb 10, 2023
1 parent 8383d87 commit ea77a29
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public final class AccountAttributes extends EntityAttributes<Account> {
private String email;
private Map<String, Instant> readNotifications;
private Instant createdAt;
private boolean isMigrated;

private AccountAttributes(String googleId) {
this.googleId = googleId;
Expand All @@ -38,6 +39,7 @@ public static AccountAttributes valueOf(Account a) {
accountAttributes.email = a.getEmail();
accountAttributes.readNotifications = a.getReadNotifications();
accountAttributes.createdAt = a.getCreatedAt();
accountAttributes.isMigrated = a.isMigrated();

return accountAttributes;
}
Expand All @@ -59,6 +61,7 @@ public AccountAttributes getCopy() {
accountAttributes.email = this.email;
accountAttributes.readNotifications = this.readNotifications;
accountAttributes.createdAt = this.createdAt;
accountAttributes.isMigrated = this.isMigrated;

return accountAttributes;
}
Expand Down Expand Up @@ -103,6 +106,14 @@ public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}

public boolean isMigrated() {
return isMigrated;
}

public void setMigrated(boolean migrated) {
isMigrated = migrated;
}

@Override
public List<String> getInvalidityInfo() {
List<String> errors = new ArrayList<>();
Expand All @@ -120,13 +131,13 @@ public List<String> getInvalidityInfo() {

@Override
public Account toEntity() {
return new Account(googleId, name, email, readNotifications);
return new Account(googleId, name, email, readNotifications, isMigrated);
}

@Override
public String toString() {
return "AccountAttributes [googleId=" + googleId + ", name=" + name
+ ", email=" + email + "]";
+ ", email=" + email + "]" + ", isMigrated=" + isMigrated + "]";
}

@Override
Expand Down Expand Up @@ -214,6 +225,7 @@ public static class UpdateOptions {
private String googleId;

private UpdateOption<Map<String, Instant>> readNotificationsOption = UpdateOption.empty();
private UpdateOption<Boolean> migratedOption = UpdateOption.empty();

private UpdateOptions(String googleId) {
assert googleId != null;
Expand All @@ -230,6 +242,7 @@ public String toString() {
return "AccountAttributes.UpdateOptions ["
+ "googleId = " + googleId
+ ", readNotifications = " + JsonUtils.toJson(readNotificationsOption)
+ ", isMigrated = " + migratedOption
+ "]";
}

Expand Down Expand Up @@ -272,6 +285,11 @@ public B withReadNotifications(Map<String, Instant> readNotifications) {
return thisBuilder;
}

public B withMigrated(boolean isMigrated) {
updateOptions.migratedOption = UpdateOption.of(isMigrated);
return thisBuilder;
}

public abstract T build();
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/teammates/storage/api/AccountsDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ public AccountAttributes updateAccount(AccountAttributes.UpdateOptions updateOpt
}

// update only if change
boolean hasSameAttributes = this.<Map<String, Instant>>hasSameValue(account.getReadNotifications(),
newAttributes.getReadNotifications());
boolean hasSameAttributes =
this.<Map<String, Instant>>hasSameValue(account.getReadNotifications(), newAttributes.getReadNotifications())
&& this.hasSameValue(account.isMigrated(), newAttributes.isMigrated());
if (hasSameAttributes) {
log.info(String.format(OPTIMIZED_SAVING_POLICY_APPLIED, Account.class.getSimpleName(), updateOptions));
return newAttributes;
}

account.setReadNotifications(newAttributes.getReadNotifications());
account.setMigrated(newAttributes.isMigrated());

saveEntity(account);

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/teammates/storage/api/CoursesDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public CourseAttributes updateCourse(CourseAttributes.UpdateOptions updateOption
boolean hasSameAttributes =
this.<String>hasSameValue(course.getName(), newAttributes.getName())
&& this.<String>hasSameValue(course.getInstitute(), newAttributes.getInstitute())
&& this.<String>hasSameValue(course.getTimeZone(), newAttributes.getTimeZone());
&& this.<String>hasSameValue(course.getTimeZone(), newAttributes.getTimeZone())
&& this.hasSameValue(course.isMigrated(), newAttributes.isMigrated());
if (hasSameAttributes) {
log.info(String.format(OPTIMIZED_SAVING_POLICY_APPLIED, Course.class.getSimpleName(), updateOptions));
return newAttributes;
Expand All @@ -89,6 +90,7 @@ public CourseAttributes updateCourse(CourseAttributes.UpdateOptions updateOption
course.setName(newAttributes.getName());
course.setTimeZone(newAttributes.getTimeZone());
course.setInstitute(newAttributes.getInstitute());
course.setMigrated(newAttributes.isMigrated());

saveEntity(course);

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/teammates/storage/entity/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Account extends BaseEntity {

private String email;

private boolean isMigrated;

@Unindex
@Serialize
private Map<String, Instant> readNotifications;
Expand All @@ -45,12 +47,15 @@ private Account() {
* @param email The official email of the user.
* @param readNotifications The notifications that the user has read, stored in a map of ID to end time.
*/
public Account(String googleId, String name, String email, Map<String, Instant> readNotifications) {
public Account(
String googleId, String name, String email,
Map<String, Instant> readNotifications, boolean isMigrated) {
this.setGoogleId(googleId);
this.setName(name);
this.setEmail(email);
this.setReadNotifications(readNotifications);
this.setCreatedAt(Instant.now());
this.setMigrated(isMigrated);
}

public String getGoogleId() {
Expand All @@ -77,6 +82,14 @@ public void setEmail(String email) {
this.email = email;
}

public void setMigrated(boolean migrated) {
isMigrated = migrated;
}

public boolean isMigrated() {
return isMigrated;
}

/**
* Retrieves the account's read notifications map.
* Returns an empty map if the account does not yet have the readNotifications attribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testGetInvalidStateInfo() throws Exception {
public void testToEntity() {
AccountAttributes account = createValidAccountAttributesObject();
Account expectedAccount = new Account(account.getGoogleId(), account.getName(),
account.getEmail(), account.getReadNotifications());
account.getEmail(), account.getReadNotifications(), false);

Account actualAccount = account.toEntity();

Expand Down Expand Up @@ -134,7 +134,7 @@ public void testBuilder_withNullArguments_shouldThrowException() {

@Test
public void testValueOf() {
Account genericAccount = new Account("id", "Joe", "joe@example.com", new HashMap<>());
Account genericAccount = new Account("id", "Joe", "joe@example.com", new HashMap<>(), false);

AccountAttributes observedAccountAttributes = AccountAttributes.valueOf(genericAccount);

Expand Down

0 comments on commit ea77a29

Please sign in to comment.