From 2b8f6d855b9c80692ee424b06d18aa06aef7b25e Mon Sep 17 00:00:00 2001 From: dao ngoc hieu <53283766+daongochieu2810@users.noreply.github.com> Date: Fri, 10 Feb 2023 20:28:09 +0800 Subject: [PATCH] [#12048] Add is migrated flag to datastore account (#12070) --- .../attributes/AccountAttributes.java | 22 +++++++++++++++++-- .../teammates/storage/api/AccountsDb.java | 6 +++-- .../java/teammates/storage/api/CoursesDb.java | 4 +++- .../teammates/storage/entity/Account.java | 15 ++++++++++++- .../attributes/AccountAttributesTest.java | 4 ++-- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main/java/teammates/common/datatransfer/attributes/AccountAttributes.java b/src/main/java/teammates/common/datatransfer/attributes/AccountAttributes.java index e557e1c4574..00598d9dc71 100644 --- a/src/main/java/teammates/common/datatransfer/attributes/AccountAttributes.java +++ b/src/main/java/teammates/common/datatransfer/attributes/AccountAttributes.java @@ -22,6 +22,7 @@ public final class AccountAttributes extends EntityAttributes { private String email; private Map readNotifications; private Instant createdAt; + private boolean isMigrated; private AccountAttributes(String googleId) { this.googleId = googleId; @@ -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; } @@ -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; } @@ -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 getInvalidityInfo() { List errors = new ArrayList<>(); @@ -120,13 +131,13 @@ public List 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 @@ -214,6 +225,7 @@ public static class UpdateOptions { private String googleId; private UpdateOption> readNotificationsOption = UpdateOption.empty(); + private UpdateOption migratedOption = UpdateOption.empty(); private UpdateOptions(String googleId) { assert googleId != null; @@ -230,6 +242,7 @@ public String toString() { return "AccountAttributes.UpdateOptions [" + "googleId = " + googleId + ", readNotifications = " + JsonUtils.toJson(readNotificationsOption) + + ", isMigrated = " + migratedOption + "]"; } @@ -272,6 +285,11 @@ public B withReadNotifications(Map readNotifications) { return thisBuilder; } + public B withMigrated(boolean isMigrated) { + updateOptions.migratedOption = UpdateOption.of(isMigrated); + return thisBuilder; + } + public abstract T build(); } diff --git a/src/main/java/teammates/storage/api/AccountsDb.java b/src/main/java/teammates/storage/api/AccountsDb.java index 47b77ab125b..0277a63f374 100644 --- a/src/main/java/teammates/storage/api/AccountsDb.java +++ b/src/main/java/teammates/storage/api/AccountsDb.java @@ -77,14 +77,16 @@ public AccountAttributes updateAccount(AccountAttributes.UpdateOptions updateOpt } // update only if change - boolean hasSameAttributes = this.>hasSameValue(account.getReadNotifications(), - newAttributes.getReadNotifications()); + boolean hasSameAttributes = + this.>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); diff --git a/src/main/java/teammates/storage/api/CoursesDb.java b/src/main/java/teammates/storage/api/CoursesDb.java index bd237bb14a7..b1597680668 100644 --- a/src/main/java/teammates/storage/api/CoursesDb.java +++ b/src/main/java/teammates/storage/api/CoursesDb.java @@ -80,7 +80,8 @@ public CourseAttributes updateCourse(CourseAttributes.UpdateOptions updateOption boolean hasSameAttributes = this.hasSameValue(course.getName(), newAttributes.getName()) && this.hasSameValue(course.getInstitute(), newAttributes.getInstitute()) - && this.hasSameValue(course.getTimeZone(), newAttributes.getTimeZone()); + && this.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; @@ -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); diff --git a/src/main/java/teammates/storage/entity/Account.java b/src/main/java/teammates/storage/entity/Account.java index 79c19375d9f..1c71f68313a 100644 --- a/src/main/java/teammates/storage/entity/Account.java +++ b/src/main/java/teammates/storage/entity/Account.java @@ -25,6 +25,8 @@ public class Account extends BaseEntity { private String email; + private boolean isMigrated; + @Unindex @Serialize private Map readNotifications; @@ -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 readNotifications) { + public Account( + String googleId, String name, String email, + Map 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() { @@ -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. diff --git a/src/test/java/teammates/common/datatransfer/attributes/AccountAttributesTest.java b/src/test/java/teammates/common/datatransfer/attributes/AccountAttributesTest.java index 62d18df9285..d651fb4249c 100644 --- a/src/test/java/teammates/common/datatransfer/attributes/AccountAttributesTest.java +++ b/src/test/java/teammates/common/datatransfer/attributes/AccountAttributesTest.java @@ -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(); @@ -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);