feat(core): Email recipients on resource shared (#8408)

This commit is contained in:
Iván Ovejero
2024-01-23 12:03:59 +01:00
committed by GitHub
parent ae06fdeb62
commit a0a1830696
10 changed files with 194 additions and 3 deletions

View File

@@ -15,6 +15,11 @@ import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { UnauthorizedError } from '@/errors/response-errors/unauthorized.error';
import { CredentialsRepository } from '@/databases/repositories/credentials.repository';
import * as utils from '@/utils';
import { UserRepository } from '@/databases/repositories/user.repository';
import { UserManagementMailer } from '@/UserManagement/email';
import { UrlService } from '@/services/url.service';
import { Logger } from '@/Logger';
import { InternalServerError } from '@/errors/response-errors/internal-server.error';
export const EECredentialsController = express.Router();
@@ -185,5 +190,37 @@ EECredentialsController.put(
user_ids_sharees_added: newShareeIds,
sharees_removed: amountRemoved,
});
const recipients = await Container.get(UserRepository).getEmailsByIds(newShareeIds);
if (recipients.length === 0) return;
try {
await Container.get(UserManagementMailer).notifyCredentialsShared({
sharerFirstName: req.user.firstName,
credentialsName: credential.name,
recipientEmails: recipients.map(({ email }) => email),
baseUrl: Container.get(UrlService).getInstanceBaseUrl(),
});
} catch (error) {
void Container.get(InternalHooks).onEmailFailed({
user: req.user,
message_type: 'Credentials shared',
public_api: false,
});
if (error instanceof Error) {
throw new InternalServerError(`Please contact your administrator: ${error.message}`);
}
}
Container.get(Logger).info('Sent credentials shared email successfully', {
sharerId: req.user.id,
});
void Container.get(InternalHooks).onUserTransactionalEmail({
user_id: req.user.id,
message_type: 'Credentials shared',
public_api: false,
});
}),
);