Skip to content

Commit

Permalink
Merge branch 'feature/215-allow-autoread-disable' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Marthym committed Oct 22, 2024
2 parents 593e629 + 5227458 commit d387948
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Locale;

public record UserSettings(
Locale preferredLocale
Locale preferredLocale,
boolean autoread
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
@Mapper(componentModel = "spring")
public interface UserSettingsMapper {
default Entity<UserSettings> getUserSettingsEntity(Record r) {
return Entity.identify(new UserSettings(Locale.forLanguageTag(r.get(USERS_SETTINGS.USSE_PREFERRED_LOCALE))))
.withId(r.get(USERS_SETTINGS.USSE_USER_ID));
return Entity.identify(new UserSettings(
Locale.forLanguageTag(r.get(USERS_SETTINGS.USSE_PREFERRED_LOCALE)),
r.get(USERS_SETTINGS.USSE_AUTOREAD))
).withId(r.get(USERS_SETTINGS.USSE_USER_ID));
}

default UsersSettingsRecord getUserSettingsRecord(UserSettings settings) {
return USERS_SETTINGS.newRecord()
.setUssePreferredLocale(settings.preferredLocale().toLanguageTag());
.setUssePreferredLocale(settings.preferredLocale().toLanguageTag())
.setUsseAutoread(settings.autoread());
}

UserSettings get(UserSettingsForm form);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Locale;

public record UserSettingsForm(
Locale preferredLocale
Locale preferredLocale,
boolean autoread
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table USERS_SETTINGS
add COLUMN USSE_AUTOREAD boolean not null default true;
2 changes: 2 additions & 0 deletions sandside/src/main/resources/graphql/security/_model.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ type User {
type UserSettings {
_id: ID
preferredLocale: String
autoread: Boolean
}

input UserSettingsForm {
preferredLocale: String
autoread: Boolean
}

type Session {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserSettingsServiceImplTest {
@BeforeEach
void setUp() {
UserSettingsPersistencePort mockPersistence = mock(UserSettingsPersistencePort.class);
doReturn(Mono.just(Entity.identify(new UserSettings(Locale.FRENCH)).withId(UserSamples.LUKE.id())))
doReturn(Mono.just(Entity.identify(new UserSettings(Locale.FRENCH, true)).withId(UserSamples.LUKE.id())))
.when(mockPersistence).get(anyString());

doAnswer(answer -> {
Expand Down Expand Up @@ -70,7 +70,7 @@ void should_fail_get_settings_on_unauthenticated() {
void should_update_user_settings() {
doReturn(Mono.just(UserSamples.LUKE)).when(mockAuthentication).getConnectedUser();

StepVerifier.create(tested.update(UserSamples.LUKE.id(), new UserSettings(Locale.FRENCH)))
StepVerifier.create(tested.update(UserSamples.LUKE.id(), new UserSettings(Locale.FRENCH, true)))
.assertNext(actual -> Assertions.assertThat(actual.id()).isEqualTo(UserSamples.LUKE.id()))
.verifyComplete();
}
Expand All @@ -79,15 +79,15 @@ void should_update_user_settings() {
void should_fail_update_settings_on_unauthorize() {
doReturn(Mono.just(UserSamples.LUKE)).when(mockAuthentication).getConnectedUser();

StepVerifier.create(tested.update(UserSamples.OBIWAN.id(), new UserSettings(Locale.FRENCH)))
StepVerifier.create(tested.update(UserSamples.OBIWAN.id(), new UserSettings(Locale.FRENCH, true)))
.verifyError(UnauthorizedException.class);
}

@Test
void should_fail_update_settings_on_unauthenticated() {
doReturn(Mono.empty()).when(mockAuthentication).getConnectedUser();

StepVerifier.create(tested.update(UserSamples.OBIWAN.id(), new UserSettings(Locale.FRENCH)))
StepVerifier.create(tested.update(UserSamples.OBIWAN.id(), new UserSettings(Locale.FRENCH, true)))
.verifyError(UnauthenticatedUser.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void should_create_settings(DSLContext dsl) {
USERS_SETTINGS.USSE_USER_ID.eq(DSIDIOUS_SETTINGS.getUsseUserId()));
Assertions.assertThat(count).isZero();
}
StepVerifier.create(tested.persist(DSIDIOUS_SETTINGS.getUsseUserId(), new UserSettings(Locale.GERMANY)))
StepVerifier.create(tested.persist(DSIDIOUS_SETTINGS.getUsseUserId(), new UserSettings(Locale.GERMANY, true)))
.assertNext(actual -> Assertions.assertThat(actual.self().preferredLocale())
.isEqualTo(Locale.GERMANY))
.verifyComplete();
Expand All @@ -82,7 +82,7 @@ void should_update_settings(DSLContext dsl) {
USERS_SETTINGS.USSE_USER_ID.eq(OKENOBI_SETTINGS.getUsseUserId()));
Assertions.assertThat(count).isOne();
}
StepVerifier.create(tested.persist(OKENOBI_SETTINGS.getUsseUserId(), new UserSettings(Locale.GERMANY)))
StepVerifier.create(tested.persist(OKENOBI_SETTINGS.getUsseUserId(), new UserSettings(Locale.GERMANY, true)))
.assertNext(actual -> Assertions.assertThat(actual.self().preferredLocale())
.isEqualTo(Locale.GERMANY))
.verifyComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ public class UsersSettingsRecordSamples implements RelationalDataSet<UsersSettin

public static final UsersSettingsRecord OKENOBI_SETTINGS = UsersSettings.USERS_SETTINGS.newRecord()
.setUsseUserId(UsersRecordSamples.OKENOBI.getUserId())
.setUssePreferredLocale(Locale.FRANCE.toLanguageTag());
.setUssePreferredLocale(Locale.FRANCE.toLanguageTag())
.setUsseAutoread(true);

public static final UsersSettingsRecord LSKYWALKER_SETTINGS = UsersSettings.USERS_SETTINGS.newRecord()
.setUsseUserId(UsersRecordSamples.LSKYWALKER.getUserId())
.setUssePreferredLocale(Locale.JAPAN.toLanguageTag());
.setUssePreferredLocale(Locale.JAPAN.toLanguageTag())
.setUsseAutoread(true);

public static final UsersSettingsRecord DSIDIOUS_SETTINGS = UsersSettings.USERS_SETTINGS.newRecord()
.setUsseUserId(UsersRecordSamples.DSIDIOUS.getUserId())
.setUssePreferredLocale(Locale.ENGLISH.toLanguageTag());
.setUssePreferredLocale(Locale.ENGLISH.toLanguageTag())
.setUsseAutoread(false);

@Override
public List<UsersSettingsRecord> records() {
Expand Down
11 changes: 10 additions & 1 deletion seaside/src/configuration/components/SettingsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
<div class="label">
<span class="label-text first-letter:capitalize">{{ t('config.settings.form.preferredLocale') }}</span>
</div>
<select v-model="userSettings.preferredLocale" class="select select-bordered w-full max-w-xs">
<select v-model="userSettings.preferredLocale" class="select select-bordered w-full">
<option disabled></option>
<option value="en-US">English (en-US)</option>
<option value="fr-FR">Français (fr-FR)</option>
</select>
<label class="label cursor-pointer mt-4">
<span class="label-text capitalize">{{ t('config.settings.form.autoread.label') }}</span>
<input v-model="userSettings.autoread" class="toggle" type="checkbox"/>
</label>
<div class="label label-text-alt pt-0">{{ t('config.settings.form.autoread.alt') }}</div>
<button class="btn btn-primary capitalize mt-5" @click.stop="onClickSaveSettings()">
{{ t('config.settings.form.action.save') }}
</button>
Expand All @@ -23,6 +28,8 @@ import { userSettingsGet, userSettingsUpdate } from '@/security/services/UserSet
import { useStore } from 'vuex';
import notificationService from '@/services/notification/NotificationService';
import { from, switchMap } from 'rxjs';
import { store } from '@/store';
import { UPDATE_SETTINGS_MUTATION as USER_UPDATE_SETTINGS_MUTATION } from '@/security/store/UserConstants';
@Component({
setup() {
Expand All @@ -38,6 +45,7 @@ export default class SettingsTab extends Vue {
private mergeLocaleMessage;
private userSettings: UserSettings = {
preferredLocale: this.locale,
autoread: true,
} as UserSettings;
private mounted(): void {
Expand All @@ -61,6 +69,7 @@ export default class SettingsTab extends Vue {
this.mergeLocaleMessage(this.userSettings.preferredLocale, messages[this.userSettings.preferredLocale.replace('-', '_')]);
this.locale = this.userSettings.preferredLocale;
notificationService.pushSimpleOk(this.t('config.settings.messages.settingsUpdateSuccessfully'));
store.commit(USER_UPDATE_SETTINGS_MUTATION, this.userSettings);
},
error: err => notificationService.pushSimpleError(this.t('config.settings.messages.unableToUpdate')),
});
Expand Down
3 changes: 3 additions & 0 deletions seaside/src/locales/config-settings_en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { en_US as config_en_US } from '@/locales/config_en-US';
export const en_US = {
...config_en_US,
'config.settings.form.preferredLocale': 'Choose your preferred language',
'config.settings.form.autoread.label': 'news auto read',
'config.settings.form.autoread.alt': 'In the article list, ' +
'autoread marks articles as read as soon as you move on to the next one.',
'config.settings.form.action.save': 'Save',
'config.settings.messages.settingsUpdateSuccessfully': 'Settings updated successfully',
'config.settings.messages.unableToUpdate': 'Unable to update settings',
Expand Down
3 changes: 3 additions & 0 deletions seaside/src/locales/config-settings_fr-FR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { fr_FR as config_fr_FR } from '@/locales/config_fr-FR';
export const fr_FR = {
...config_fr_FR,
'config.settings.form.preferredLocale': 'choisissez votre langue préférée :',
'config.settings.form.autoread.label': 'lecture automatique des articles',
'config.settings.form.autoread.alt': 'Dans la liste des articles, ' +
'l’autoread marque les articles comme lu dés que vous passez au suivant.',
'config.settings.form.action.save': 'enregistrer',
'config.settings.messages.settingsUpdateSuccessfully': 'Paramètres enregistrés avec succès.',
'config.settings.messages.unableToUpdate': 'Erreur lors de l’enregistrement des paramètres !',
Expand Down
1 change: 1 addition & 0 deletions seaside/src/security/model/UserSettings.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { Locale } from '@/common/model/Locale.type';

export type UserSettings = {
preferredLocale: Locale,
autoread: boolean,
}
2 changes: 2 additions & 0 deletions seaside/src/security/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { refresh } from '@/security/services/AuthenticationService';
import {
LOGOUT_MUTATION as USER_LOGOUT_MUTATION,
UPDATE_MUTATION as USER_UPDATE_MUTATION,
UPDATE_SETTINGS_MUTATION as USER_UPDATE_SETTINGS_MUTATION,
} from '@/security/store/UserConstants';

const LoginPage = () => import('@/security/pages/LoginPage.vue');
Expand All @@ -24,6 +25,7 @@ export const requireAuthNavGuard: NavigationGuardWithThis<NavigationGuardWithThi
i18n.global.locale.value = session.settings.preferredLocale;
}
store.commit(USER_UPDATE_MUTATION, session.user);
store.commit(USER_UPDATE_SETTINGS_MUTATION, session.settings);
} catch (err) {
store.commit(USER_LOGOUT_MUTATION);
}
Expand Down
2 changes: 1 addition & 1 deletion seaside/src/security/services/AuthenticationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const SESSION_REFRESH_REQUEST = `#graphql
mutation {
refreshSession {
user { _id login mail name roles }
settings { preferredLocale }
settings { preferredLocale autoread }
maxAge
}
}`;
Expand Down
4 changes: 2 additions & 2 deletions seaside/src/security/services/UserSettingsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UserSettings } from '@/security/model/UserSettings.type';
const USERS_SETTINGS_GET_REQUEST = `#graphql
query UserSettingsGet($id: ID){
userSettingsGet(userId: $id) {
preferredLocale
preferredLocale autoread
}
}`;

Expand All @@ -20,7 +20,7 @@ export function userSettingsGet(userId: string): Observable<UserSettings> {
const USERS_SETTINGS_UPDATE_REQUEST = `#graphql
mutation UserSettingsUpdate($id: ID, $settings: UserSettingsForm){
userSettingsUpdate(userId: $id, settings: $settings) {
preferredLocale
preferredLocale autoread
}
}`;

Expand Down
2 changes: 2 additions & 0 deletions seaside/src/security/store/UserConstants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const NAMESPACE = 'user';
export const LOGOUT = 'logout';
export const UPDATE = 'update';
export const UPDATE_SETTINGS = 'updateSettings';
export const OPEN_CREATE_ACCOUNT = 'openCreateAccount';
export const CLOSE_CREATE_ACCOUNT = 'closeCreateAccount';
export const ADD_ROLE = 'addRole';
Expand All @@ -9,6 +10,7 @@ export const HAS_ROLE_MANAGER = 'hasRoleManager';
export const HAS_ROLE_ADMIN = 'hasRoleAdmin';
export const LOGOUT_MUTATION = `${NAMESPACE}/${LOGOUT}`;
export const UPDATE_MUTATION = `${NAMESPACE}/${UPDATE}`;
export const UPDATE_SETTINGS_MUTATION = `${NAMESPACE}/${UPDATE_SETTINGS}`;
export const OPEN_CREATE_ACCOUNT_MUTATION = `${NAMESPACE}/${OPEN_CREATE_ACCOUNT}`;
export const CLOSE_CREATE_ACCOUNT_MUTATION = `${NAMESPACE}/${CLOSE_CREATE_ACCOUNT}`;
export const USER_ADD_ROLE_MUTATION = `${NAMESPACE}/${ADD_ROLE}`;
Expand Down
7 changes: 7 additions & 0 deletions seaside/src/security/store/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import {
LOGOUT,
OPEN_CREATE_ACCOUNT,
UPDATE,
UPDATE_SETTINGS,
} from '@/security/store/UserConstants';
import { GetterTree } from 'vuex';
import { UserSettings } from '@/security/model/UserSettings.type';

export type UserState = {
user: User;
isAuthenticated: boolean | undefined;
isCreateAccountOpen: boolean;
autoread: boolean;
}

const state = (): UserState => ({
user: ANONYMOUS,
isAuthenticated: undefined,
isCreateAccountOpen: false,
autoread: true,
});

// getters
Expand Down Expand Up @@ -59,6 +63,9 @@ const mutations = {
st.user = payload;
st.isAuthenticated = hasRole(st.user, UserRole.USER);
},
[UPDATE_SETTINGS](st: UserState, payload: UserSettings): void {
st.autoread = payload.autoread;
},
};

const hasRole = (user: User, expectedRole: UserRole, entity?: string): boolean => {
Expand Down
Loading

0 comments on commit d387948

Please sign in to comment.