Skip to content

Commit

Permalink
Merge pull request #123 from ezpaarse-project:feature/drop-repo-id
Browse files Browse the repository at this point in the history
feature: drop repos id
  • Loading branch information
nojhamster authored Nov 10, 2023
2 parents 0aaf7fd + 2b3d287 commit 47ea292
Show file tree
Hide file tree
Showing 18 changed files with 447 additions and 368 deletions.
48 changes: 39 additions & 9 deletions api/lib/controllers/institutions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,38 +291,68 @@ exports.importInstitutions = async (ctx) => {
...item,
logo: undefined,
logoId: base64logo ? await imagesService.storeLogo(base64logo) : undefined,

spaces: {
connectOrCreate: item.spaces?.map?.((spaceData) => ({
where: { id: spaceData.id },
create: spaceData,
})),
},

repositories: {
connectOrCreate: item.repositories?.map?.((repoData) => ({
where: {
institutionId_pattern: {
institutionId: item.id,
pattern: repoData?.pattern,
},
},
where: { pattern: repoData.pattern },
create: repoData,
})),
},

sushiCredentials: {
connectOrCreate: item.sushiCredentials?.map?.((sushi) => ({
where: { id: sushi.id },
create: { ...sushi, institutionId: undefined },
})),
},

memberships: {
connectOrCreate: item.memberships?.map?.((membership) => ({
where: {
username_institutionId: {
institutionId: membership.institutionId,
username: membership.username,
institutionId: item.id,
username: membership?.username,
},
},
create: {
...(membership ?? {}),
username: undefined,

user: {
connect: { username: membership?.username },
},

spacePermissions: {
connectOrCreate: membership?.spacePermissions?.map?.((perm) => ({
where: {
username_spaceId: {
username: membership?.username,
spaceId: perm?.spaceId,
},
},
create: perm,
})),
},

repositoryPermissions: {
connectOrCreate: membership?.repositoryPermissions?.map?.((perm) => ({
where: {
username_repositoryPattern: {
username: membership?.username,
repositoryPattern: perm?.repositoryPattern,
},
},
create: perm,
})),
},
},
create: { ...membership, institutionId: undefined },
})),
},
};
Expand Down
26 changes: 14 additions & 12 deletions api/lib/controllers/repositories/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ exports.updateOne = async (ctx) => {
const { repository } = ctx.state;
const { body } = ctx.request;

let updatedSushiCredentials;
let updatedRepository;

try {
updatedSushiCredentials = await repositoriesService.update({
where: { id: repository.id },
updatedRepository = await repositoriesService.update({
where: { pattern: repository.pattern },
data: body,
});
} catch (e) {
Expand All @@ -89,17 +89,19 @@ exports.updateOne = async (ctx) => {
break;
default:
}
} else {
throw e;
}
}

ctx.status = 200;
ctx.body = updatedSushiCredentials;
ctx.body = updatedRepository;
};

exports.deleteOne = async (ctx) => {
const { repositoryId } = ctx.params;
const { pattern } = ctx.params;

await repositoriesService.delete({ where: { id: repositoryId } });
await repositoriesService.delete({ where: { pattern } });

ctx.status = 204;
};
Expand All @@ -111,13 +113,13 @@ exports.upsertPermission = async (ctx) => {
const { value: body } = permissionUpsertSchema.validate({
...ctx.request.body,
institutionId: repository.institutionId,
repositoryId: repository.id,
pattern: repository.pattern,
username,
});

const permissionData = {
...body,
repository: { connect: { id: repository.id } },
repository: { connect: { pattern: repository.pattern } },
membership: {
connect: {
username_institutionId: {
Expand All @@ -130,9 +132,9 @@ exports.upsertPermission = async (ctx) => {

const updatedPermissions = await repoPermissionsService.upsert({
where: {
username_repositoryId: {
username_repositoryPattern: {
username,
repositoryId: repository.id,
repositoryPattern: repository.pattern,
},
},
create: permissionData,
Expand All @@ -149,9 +151,9 @@ exports.deletePermission = async (ctx) => {

const updatedPermissions = await repoPermissionsService.delete({
where: {
username_repositoryId: {
username_repositoryPattern: {
username,
repositoryId: repository.id,
repositoryPattern: repository.pattern,
},
},
});
Expand Down
18 changes: 9 additions & 9 deletions api/lib/controllers/repositories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ router.use(requireJwt, requireUser);

router.route({
method: 'PUT',
path: '/:repositoryId/permissions/:username',
path: '/:pattern/permissions/:username',
handler: [
fetchRepository(),
fetchInstitution({ getId: (ctx) => ctx?.state?.repository?.institutionId }),
Expand All @@ -40,15 +40,15 @@ router.route({
validate: {
type: 'json',
params: {
repositoryId: Joi.string().trim().required(),
pattern: Joi.string().trim().required(),
username: Joi.string().trim().required(),
},
},
});

router.route({
method: 'DELETE',
path: '/:repositoryId/permissions/:username',
path: '/:pattern/permissions/:username',
handler: [
fetchRepository(),
fetchInstitution({ getId: (ctx) => ctx?.state?.repository?.institutionId }),
Expand All @@ -57,7 +57,7 @@ router.route({
],
validate: {
params: {
repositoryId: Joi.string().trim().required(),
pattern: Joi.string().trim().required(),
username: Joi.string().trim().required(),
},
},
Expand All @@ -80,7 +80,7 @@ router.get('/', {

router.route({
method: 'GET',
path: '/:repositoryId',
path: '/:pattern',
handler: [
fetchRepository(),
getOne,
Expand All @@ -99,7 +99,7 @@ router.route({

router.route({
method: 'PATCH',
path: '/:repositoryId',
path: '/:pattern',
handler: [
fetchRepository(),
updateOne,
Expand All @@ -108,21 +108,21 @@ router.route({
type: 'json',
body: adminUpdateSchema,
params: {
repositoryId: Joi.string().trim().required(),
pattern: Joi.string().trim().required(),
},
},
});

router.route({
method: 'DELETE',
path: '/:repositoryId',
path: '/:pattern',
handler: [
fetchRepository(),
deleteOne,
],
validate: {
params: {
repositoryId: Joi.string().trim().required(),
pattern: Joi.string().trim().required(),
},
},
});
Expand Down
36 changes: 34 additions & 2 deletions api/lib/controllers/users/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,47 @@ exports.importUsers = async (ctx) => {
email: item.email,
isAdmin: !!item.isAdmin,
metadata: item.metadata,

memberships: {
connectOrCreate: item.memberships?.map?.((membership) => ({
where: {
username_institutionId: {
username: item.username,
institutionId: membership?.institutionId,
username: membership?.username,
},
},
create: { ...membership, username: undefined },
create: {
...(membership ?? {}),
institutionId: undefined,

institution: {
connect: { id: membership?.institutionId },
},

spacePermissions: {
connectOrCreate: membership?.spacePermissions?.map?.((perm) => ({
where: {
username_spaceId: {
username: item.username,
spaceId: perm?.spaceId,
},
},
create: perm,
})),
},

repositoryPermissions: {
connectOrCreate: membership?.repositoryPermissions?.map?.((perm) => ({
where: {
username_repositoryPattern: {
username: item.username,
repositoryPattern: perm?.repositoryPattern,
},
},
create: perm,
})),
},
},
})),
},
};
Expand Down
6 changes: 3 additions & 3 deletions api/lib/entities/repository-permissions.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const schema = {
institutionId: Joi.string().trim(),
membership: Joi.object(),

repositoryId: Joi.string().trim(),
pattern: Joi.string().trim(),
repository: Joi.object(),

readonly: Joi.boolean(),
Expand All @@ -30,7 +30,7 @@ const immutableFields = [
'username',
'institutionId',
'membership',
'repositoryId',
'pattern',
'repository',
];

Expand All @@ -47,7 +47,7 @@ const includableFields = [
*/
const upsertSchema = withModifiers(
schema,
requireFields(['username', 'repositoryId', 'institutionId']),
requireFields(['username', 'pattern', 'institutionId']),
ignoreFields(immutableFields),
withDefaults({
readonly: false,
Expand Down
3 changes: 2 additions & 1 deletion api/lib/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function fetchModel(modelName, opts = {}) {
break;

case 'repository':
findOptions.where = { pattern: modelId };
item = modelId && await RepositoriesService.findUnique(findOptions);
break;

Expand Down Expand Up @@ -217,6 +218,6 @@ module.exports = {
fetchInstitution: (opts = {}) => fetchModel('institution', { state: 'institution', ...opts }),
fetchSushi: (opts = {}) => fetchModel('sushi', { state: 'sushi', ...opts }),
fetchSushiEndpoint: (opts = {}) => fetchModel('sushi-endpoint', { state: 'endpoint', params: 'endpointId', ...opts }),
fetchRepository: (opts = {}) => fetchModel('repository', { state: 'repository', params: 'repositoryId', ...opts }),
fetchRepository: (opts = {}) => fetchModel('repository', { state: 'repository', params: 'pattern', ...opts }),
fetchSpace: (opts = {}) => fetchModel('space', { state: 'space', params: 'spaceId', ...opts }),
};
1 change: 1 addition & 0 deletions api/lib/services/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ exports.save = async (ctx) => {
case 'sushi/download-report':
case 'sushi/harvest':
case 'sushi/import':
case 'institutions/import':
case 'sushi/check-connection':
if (metric.response.body && !metric.response.body.error) {
metric.response.body = null;
Expand Down
14 changes: 5 additions & 9 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ model SpacePermission {

/// A repository (a section of elasticsearch allocated to an institution)
model Repository {
/// ID of the repository
id String @id @default(cuid())
/// ID of the institution
institutionId String?
/// The institution this repository is associated to
Expand All @@ -171,13 +169,11 @@ model Repository {
/// Latest update date
updatedAt DateTime @updatedAt
/// The index pattern (ex: b-bibcnrs*)
pattern String
pattern String @id
/// The repository type (ezpaarse, counter5)
type String @db.VarChar(50)
/// Member permissions associated to this repository
permissions RepositoryPermission[]
@@unique([institutionId, pattern])
}

/// A repository permission (access rights of a member for a specific repository)
Expand All @@ -188,16 +184,16 @@ model RepositoryPermission {
institutionId String
/// The member
membership Membership @relation(fields: [username, institutionId], references: [username, institutionId], onDelete: Cascade)
/// ID of the repository
repositoryId String
/// Pattern of the repository
repositoryPattern String
/// The repository
repository Repository @relation(fields: [repositoryId], references: [id], onDelete: Cascade)
repository Repository @relation(fields: [repositoryPattern], references: [pattern], onDelete: Cascade)
/// Whether the member has a readonly access to the repository
readonly Boolean @default(false)
/// Whether the permission can be modified or not
locked Boolean @default(false)
@@id([username, repositoryId])
@@id([username, repositoryPattern])
}

/// Represent the actions that are triggered
Expand Down
Loading

0 comments on commit 47ea292

Please sign in to comment.