-
Notifications
You must be signed in to change notification settings - Fork 0
Dev schoolconfing #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Anugraha-sutara
wants to merge
12
commits into
main
Choose a base branch
from
dev-schoolconfig
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dev schoolconfing #180
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
65680a0
School config work in progress.
mikedawson 684eeff
implement SchoolConfigSettingDataSource for db, http, and repository
Anugraha-sutara cf5f182
update DrainRemoteWriteQueueUseCase
Anugraha-sutara ea17d8a
add permission check query
Anugraha-sutara 96b7d4d
Merge branch 'main' into dev-schoolconfig
Anugraha-sutara 06cee38
fix build failure
Anugraha-sutara a92896f
add db migration
Anugraha-sutara b281a35
Merge branch 'main' into dev-schoolconfig
Anugraha-sutara c0fe5a3
Update SchoolConfigSettingDataSource.GetListParams to use a list of k…
Anugraha-sutara d668753
add refactor
Anugraha-sutara eebc0af
add unittest for school config
Anugraha-sutara 5402720
fix unit test
Anugraha-sutara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,803 changes: 1,803 additions & 0 deletions
1,803
respect-datalayer-db/schemas/world.respect.datalayer.db.RespectSchoolDatabase/13.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...rc/commonMain/kotlin/world/respect/datalayer/db/school/SchoolConfigSettingDataSourceDb.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package world.respect.datalayer.db.school | ||
|
|
||
| import androidx.room.Transactor | ||
| import androidx.room.useWriterConnection | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.map | ||
| import world.respect.datalayer.AuthenticatedUserPrincipalId | ||
| import world.respect.datalayer.DataLoadMetaInfo | ||
| import world.respect.datalayer.DataLoadParams | ||
| import world.respect.datalayer.DataLoadState | ||
| import world.respect.datalayer.DataReadyState | ||
| import world.respect.datalayer.NoDataLoadedState | ||
| import world.respect.datalayer.UidNumberMapper | ||
| import world.respect.datalayer.db.RespectSchoolDatabase | ||
| import world.respect.datalayer.db.school.adapters.asEntity | ||
| import world.respect.datalayer.db.school.adapters.asModel | ||
| import world.respect.datalayer.exceptions.ForbiddenException | ||
| import world.respect.datalayer.school.SchoolConfigSettingDataSource | ||
| import world.respect.datalayer.school.SchoolConfigSettingDataSourceLocal | ||
| import world.respect.datalayer.school.ext.foldToFlag | ||
| import world.respect.datalayer.school.model.SchoolConfigSetting | ||
| import world.respect.datalayer.shared.maxLastModifiedOrNull | ||
| import world.respect.datalayer.shared.maxLastStoredOrNull | ||
| import kotlin.time.Clock | ||
|
|
||
| class SchoolConfigSettingDataSourceDb( | ||
| private val schoolDb: RespectSchoolDatabase, | ||
| private val authenticatedUser: AuthenticatedUserPrincipalId, | ||
| private val uidNumberMapper: UidNumberMapper, | ||
| ) : SchoolConfigSettingDataSourceLocal { | ||
|
|
||
| private val authenticatedUserUidNum: Long | ||
| get() = uidNumberMapper(authenticatedUser.guid) | ||
|
|
||
| override suspend fun findByGuid( | ||
| params: DataLoadParams, | ||
| guid: String | ||
| ): DataLoadState<SchoolConfigSetting> { | ||
| return schoolDb.getSchoolConfigSettingEntityDao().list( | ||
| authenticatedPersonUidNum = authenticatedUserUidNum, | ||
| keys = listOf(guid) | ||
| ).firstOrNull()?.asModel()?.let { DataReadyState(it) } ?: NoDataLoadedState.notFound() | ||
| } | ||
|
|
||
| override fun listAsFlow( | ||
| loadParams: DataLoadParams, | ||
| params: SchoolConfigSettingDataSource.GetListParams | ||
| ): Flow<DataLoadState<List<SchoolConfigSetting>>> { | ||
| return schoolDb.getSchoolConfigSettingEntityDao().listAsFlow( | ||
| authenticatedPersonUidNum = authenticatedUserUidNum, | ||
| keys = params.keys, | ||
| since = params.common.since?.toEpochMilliseconds() ?: 0 | ||
| ).map { list -> | ||
| DataReadyState( | ||
| data = list.map { it.asModel() } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun list( | ||
| loadParams: DataLoadParams, | ||
| params: SchoolConfigSettingDataSource.GetListParams | ||
| ): DataLoadState<List<SchoolConfigSetting>> { | ||
| val queryTime = Clock.System.now() | ||
| val data = schoolDb.getSchoolConfigSettingEntityDao().list( | ||
| authenticatedPersonUidNum = authenticatedUserUidNum, | ||
| keys = params.keys, | ||
| since = params.common.since?.toEpochMilliseconds() ?: 0 | ||
| ).map { it.asModel() } | ||
|
|
||
| return DataReadyState( | ||
| data = data, | ||
| metaInfo = DataLoadMetaInfo( | ||
| lastModified = data.maxLastModifiedOrNull()?.toEpochMilliseconds() ?: -1, | ||
| lastStored = data.maxLastStoredOrNull()?.toEpochMilliseconds() ?: -1, | ||
| consistentThrough = queryTime, | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun store(list: List<SchoolConfigSetting>) { | ||
| if (list.isEmpty()) return | ||
| schoolDb.useWriterConnection { con -> | ||
| con.withTransaction(Transactor.SQLiteTransactionType.IMMEDIATE) { | ||
| list.forEach { setting -> | ||
| val lastModAndPermission = schoolDb.getSchoolConfigSettingEntityDao() | ||
| .getLastModifiedAndHasPermission( | ||
| authenticatedPersonUidNum = authenticatedUserUidNum, | ||
| key = setting.key, | ||
| canWriteRolesMask = setting.canWrite.foldToFlag() | ||
| ) | ||
|
|
||
| if (!lastModAndPermission.hasPermission) { | ||
| throw ForbiddenException() | ||
| } | ||
| } | ||
|
|
||
| schoolDb.getSchoolConfigSettingEntityDao().insert( | ||
| list.map { it.copy(stored = Clock.System.now()).asEntity() } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun updateLocal( | ||
| list: List<SchoolConfigSetting>, | ||
| forceOverwrite: Boolean | ||
| ) { | ||
| if (list.isEmpty()) return | ||
| schoolDb.useWriterConnection { con -> | ||
| con.withTransaction(Transactor.SQLiteTransactionType.IMMEDIATE) { | ||
| val toInsert = list.filter { item -> | ||
| forceOverwrite || schoolDb.getSchoolConfigSettingEntityDao() | ||
| .getLastModifiedByKey( | ||
| key = item.key | ||
| ).let { it ?: 0L } < item.lastModified.toEpochMilliseconds() | ||
| }.map { it.asEntity() } | ||
|
|
||
| if (toInsert.isNotEmpty()) { | ||
| schoolDb.getSchoolConfigSettingEntityDao().insert(toInsert) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun findByUidList(uids: List<String>): List<SchoolConfigSetting> { | ||
| return schoolDb.getSchoolConfigSettingEntityDao().list( | ||
| authenticatedPersonUidNum = authenticatedUserUidNum, | ||
| keys = uids | ||
| ).map { it.asModel() } | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...ommonMain/kotlin/world/respect/datalayer/db/school/adapters/SchoolConfigSettingAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package world.respect.datalayer.db.school.adapters | ||
|
|
||
| import world.respect.datalayer.db.school.entities.SchoolConfigSettingEntity | ||
| import world.respect.datalayer.school.ext.foldToFlag | ||
| import world.respect.datalayer.school.model.PersonRoleEnum | ||
| import world.respect.datalayer.school.model.SchoolConfigSetting | ||
|
|
||
|
|
||
| fun SchoolConfigSetting.asEntity(): SchoolConfigSettingEntity { | ||
| return SchoolConfigSettingEntity( | ||
| scsKey = key, | ||
| scsValue = value, | ||
| scsStatus = status, | ||
| scsLastModified = lastModified, | ||
| scsStored = stored, | ||
| scsCanReadFlags = canRead.foldToFlag(), | ||
| scsCanWriteFlags = canWrite.foldToFlag(), | ||
| scsAnonCanRead = canRead.contains(null), | ||
| ) | ||
| } | ||
|
|
||
| fun SchoolConfigSettingEntity.asModel(): SchoolConfigSetting { | ||
| return SchoolConfigSetting( | ||
| key = scsKey, | ||
| value = scsValue, | ||
| status = scsStatus, | ||
| lastModified = scsLastModified, | ||
| stored = scsStored, | ||
| canRead = PersonRoleEnum.unfoldFromFlag(scsCanReadFlags), | ||
| canWrite = PersonRoleEnum.unfoldFromFlag(scsCanWriteFlags) | ||
| ) | ||
| } |
90 changes: 90 additions & 0 deletions
90
.../commonMain/kotlin/world/respect/datalayer/db/school/daos/SchoolConfigSettingEntityDao.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package world.respect.datalayer.db.school.daos | ||
|
|
||
| import androidx.room.Dao | ||
| import androidx.room.Insert | ||
| import androidx.room.OnConflictStrategy | ||
| import androidx.room.Query | ||
| import kotlinx.coroutines.flow.Flow | ||
| import world.respect.datalayer.db.school.entities.LastModifiedAndPermission | ||
| import world.respect.datalayer.db.school.entities.SchoolConfigSettingEntity | ||
|
|
||
| @Dao | ||
| interface SchoolConfigSettingEntityDao { | ||
|
|
||
| @Insert(onConflict = OnConflictStrategy.REPLACE) | ||
| suspend fun insert(entities: List<SchoolConfigSettingEntity>) | ||
|
|
||
| @Query(LIST_SQL) | ||
| fun listAsFlow( | ||
| authenticatedPersonUidNum: Long, | ||
| keys: List<String>? = null, | ||
| since: Long = 0, | ||
| ): Flow<List<SchoolConfigSettingEntity>> | ||
|
|
||
| @Query(LIST_SQL) | ||
| suspend fun list( | ||
| authenticatedPersonUidNum: Long, | ||
| keys: List<String>? = null, | ||
| since: Long = 0, | ||
| ): List<SchoolConfigSettingEntity> | ||
|
|
||
| @Query(""" | ||
| SELECT scsLastModified | ||
| FROM SchoolConfigSettingEntity | ||
| WHERE scsKey = :key | ||
| """) | ||
| suspend fun getLastModifiedByKey(key: String): Long? | ||
|
|
||
| @Query(GET_LAST_MODIFIED_AND_HAS_PERMISSION_SQL) | ||
| suspend fun getLastModifiedAndHasPermission( | ||
| authenticatedPersonUidNum: Long, | ||
| key: String, | ||
| canWriteRolesMask: Int = 0 | ||
| ): LastModifiedAndPermission | ||
|
|
||
| companion object { | ||
|
|
||
| private const val AUTHENTICATED_USER_ROLE_SQL = """ | ||
| SELECT PersonRoleEntity.prRoleEnum | ||
| FROM PersonRoleEntity | ||
| WHERE PersonRoleEntity.prPersonGuidHash = :authenticatedPersonUidNum | ||
| LIMIT 1 | ||
| """ | ||
|
|
||
| private const val READ_PERMISSION_CHECK_SQL = """ | ||
| scsAnonCanRead OR (($AUTHENTICATED_USER_ROLE_SQL) & scsCanReadFlags) > 0 | ||
| """ | ||
|
|
||
| private const val WRITE_PERMISSION_CHECK_SQL = """ | ||
| (($AUTHENTICATED_USER_ROLE_SQL) & scsCanWriteFlags) > 0 | ||
| """ | ||
|
|
||
| private const val LIST_SQL = """ | ||
| SELECT SchoolConfigSettingEntity.* | ||
| FROM SchoolConfigSettingEntity | ||
| WHERE scsKey IN (:keys) | ||
| AND SchoolConfigSettingEntity.scsStored > :since | ||
| AND ($READ_PERMISSION_CHECK_SQL) | ||
| """ | ||
|
|
||
| private const val GET_LAST_MODIFIED_AND_HAS_PERMISSION_SQL = """ | ||
| SELECT 0 AS uidNum, | ||
| (SELECT SchoolConfigSettingEntity.scsLastModified | ||
| FROM SchoolConfigSettingEntity | ||
| WHERE SchoolConfigSettingEntity.scsKey = :key) AS lastModified, | ||
| ( | ||
| -- for existing records | ||
| EXISTS ( | ||
| SELECT 1 | ||
| FROM SchoolConfigSettingEntity | ||
| WHERE SchoolConfigSettingEntity.scsKey = :key | ||
| AND ($WRITE_PERMISSION_CHECK_SQL) | ||
| ) | ||
| OR | ||
| -- for new records (using the passed mask) | ||
| (NOT EXISTS (SELECT 1 FROM SchoolConfigSettingEntity WHERE scsKey = :key) | ||
| AND ($AUTHENTICATED_USER_ROLE_SQL) & :canWriteRolesMask > 0) | ||
| ) AS hasPermission | ||
| """ | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
...commonMain/kotlin/world/respect/datalayer/db/school/entities/SchoolConfigSettingEntity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package world.respect.datalayer.db.school.entities | ||
|
|
||
| import androidx.room.Entity | ||
| import androidx.room.PrimaryKey | ||
| import world.respect.datalayer.school.model.StatusEnum | ||
| import kotlin.time.Instant | ||
|
|
||
| @Entity | ||
| data class SchoolConfigSettingEntity( | ||
| @PrimaryKey | ||
| val scsKey: String, | ||
| val scsValue: String, | ||
| val scsStatus: StatusEnum, | ||
| val scsLastModified: Instant, | ||
| val scsStored: Instant, | ||
| val scsCanReadFlags: Int, | ||
| val scsAnonCanRead: Boolean, | ||
| val scsCanWriteFlags: Int, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.