diff --git a/src/db/mock/groups.json b/src/db/mock/groups.json new file mode 100644 index 0000000..3fa3dab --- /dev/null +++ b/src/db/mock/groups.json @@ -0,0 +1,14 @@ +[ + { + "type": "group", + "name": "user.admin" + }, + { + "type": "group", + "name": "user.member" + }, + { + "type": "group", + "name": "user.devops" + } +] diff --git a/src/db/mock/users.json b/src/db/mock/users.json new file mode 100644 index 0000000..3ee6b9e --- /dev/null +++ b/src/db/mock/users.json @@ -0,0 +1,68 @@ +[ + { + "type": "user", + "firstname": "Jean-Baptiste", + "lastname": "Bonnemaison", + "mail": "jean-baptiste.bonnemaison@u-bordeaux.fr", + "groups": [ + "user.admin" + ] + }, + { + "type": "user", + "firstname": "Pierre", + "lastname": "Grange-Praderas", + "mail": "pierre.grange-praderas@u-bordeaux.fr", + "groups": [ + "user.admin", + "user.devops" + ] + }, + { + "type": "user", + "firstname": "Eléonore", + "lastname": "Fraisse", + "mail": "eleonore.fraisse@icloud.com", + "groups": [ + "user.member" + ] + }, + { + "type": "user", + "firstname": "pgp", + "lastname": "pgp", + "mail": "pgp@cplusmoins.org", + "groups": [ + "user.admin" + ] + }, + { + "type": "user", + "firstname": "Julien", + "lastname": "Oculi", + "mail": "oculi.julien.t@gmail.com", + "groups": [ + "user.admin", + "user.devops" + ] + }, + { + "type": "user", + "firstname": "John", + "lastname": "Doe", + "mail": "test_coha@yopmail.com", + "groups": [ + "user.admin", + "user.devops" + ] + }, + { + "type": "user", + "firstname": "Ulrich", + "lastname": "Moniteur", + "mail": "moniteurulrich@gmail.com", + "groups": [ + "user.member" + ] + } +] diff --git a/src/db/mod.ts b/src/db/mod.ts new file mode 100644 index 0000000..5af905b --- /dev/null +++ b/src/db/mod.ts @@ -0,0 +1,42 @@ +import { Db, Group } from '@cohabit/ressources_manager/mod.ts' + +// Import Datas +import groups from './mock/groups.json' with { type: 'json' } +import users from './mock/users.json' with { type: 'json' } +import { User } from '@cohabit/ressources_manager/src/models/mod.ts' +import { MailAddress } from '@cohabit/ressources_manager/types.ts' +import { existsSync } from '$std/fs/exists.ts' + +const dbPath = './_fresh/db.sqlite' +export const db = await Db.init(dbPath) + + +//! Temp for demo only +if (!sessionStorage.getItem('db-loaded') && !existsSync(dbPath)) { + console.log('=============LOAD DB=============') + + // Load groups to DB + for (const { name } of groups) { + const group = Group.load({ name }) + db.ressource.group.set([group]) + } + + // Load users to DB + for (const { lastname, firstname, mail, groups: groupNames } of users) { + // Get groups of user + const groups = await db.ressource.group.listRef((group) => + groupNames.includes(group.name) + ) + + const user = User.load({ + firstname, + lastname, + mail: mail as MailAddress, + groups, + }) + + db.ressource.user.set([user]) + } + + sessionStorage.setItem('db-loaded', 'true') +}