0.11.19 : add simple notification
This commit is contained in:
@@ -31,7 +31,8 @@
|
||||
"admin",
|
||||
"owner",
|
||||
"follow",
|
||||
"pending"
|
||||
"pending",
|
||||
"invited"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,25 @@ export default factories.createCoreController(
|
||||
);
|
||||
membershipMeta.success = true;
|
||||
membershipMeta.membershipId = membership?.id as number;
|
||||
|
||||
try {
|
||||
await strapi.service("api::notification.notification").createNotification({
|
||||
title: "Groupe créé",
|
||||
message: `Vous avez créé un nouveau groupe`,
|
||||
type: "success",
|
||||
target_user: userId,
|
||||
source: "group-create",
|
||||
payload: {
|
||||
groupId,
|
||||
membershipId: membership?.id,
|
||||
},
|
||||
});
|
||||
} catch (notificationErr: any) {
|
||||
strapi.log.warn(
|
||||
"[group.create] Erreur création notification:",
|
||||
notificationErr
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (err: any) {
|
||||
strapi.log.error(
|
||||
@@ -97,5 +116,98 @@ export default factories.createCoreController(
|
||||
|
||||
return response;
|
||||
},
|
||||
async invite(ctx) {
|
||||
const body = ctx.request.body as any;
|
||||
const data =
|
||||
typeof body?.data === "string"
|
||||
? JSON.parse(body.data)
|
||||
: body?.data || {};
|
||||
|
||||
const groupId = ctx.params.id;
|
||||
const userIds = data?.usersIds || [];
|
||||
|
||||
if (!groupId) {
|
||||
return ctx.badRequest("Missing groupId");
|
||||
}
|
||||
|
||||
if (!Array.isArray(userIds) || userIds.length === 0) {
|
||||
return ctx.badRequest("userIds must be a non-empty array");
|
||||
}
|
||||
|
||||
const results = {
|
||||
success: [],
|
||||
errors: [],
|
||||
};
|
||||
|
||||
for (const userId of userIds) {
|
||||
try {
|
||||
const existingMembership = await strapi.entityService.findMany(
|
||||
"api::group-membership.group-membership",
|
||||
{
|
||||
filters: {
|
||||
user: userId,
|
||||
group: groupId,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (existingMembership && existingMembership.length > 0) {
|
||||
results.errors.push({
|
||||
userId,
|
||||
error: "User is already a member of this group",
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const membership = await strapi.entityService.create(
|
||||
"api::group-membership.group-membership",
|
||||
{
|
||||
data: {
|
||||
user: userId,
|
||||
group: groupId,
|
||||
role: "invited",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
try {
|
||||
await strapi.service("api::notification.notification").createNotification({
|
||||
title: "Invitation de groupe",
|
||||
message: `Vous avez été invité à rejoindre un groupe`,
|
||||
type: "info",
|
||||
target_user: userId,
|
||||
source: "group-invite",
|
||||
payload: {
|
||||
groupId,
|
||||
membershipId: membership?.id,
|
||||
},
|
||||
});
|
||||
} catch (notificationErr: any) {
|
||||
strapi.log.warn(
|
||||
"[group.invite] Erreur création notification:",
|
||||
notificationErr
|
||||
);
|
||||
}
|
||||
|
||||
results.success.push({
|
||||
userId,
|
||||
membershipId: membership?.id,
|
||||
});
|
||||
} catch (err: any) {
|
||||
strapi.log.error(
|
||||
"[group.invite] Erreur création GroupMembership:",
|
||||
err
|
||||
);
|
||||
results.errors.push({
|
||||
userId,
|
||||
error: err?.message || "Unknown error",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: results,
|
||||
};
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Custom post routes
|
||||
*/
|
||||
|
||||
export default {
|
||||
routes: [
|
||||
{
|
||||
method: "POST",
|
||||
path: "/group/:id/invite",
|
||||
handler: "group.invite",
|
||||
},
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user