Skip to content

Commit 80632c2

Browse files
committed
feat: add share link
1 parent 09a617e commit 80632c2

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/vault/vault-web.service.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ export class VaultWebService {
7676
}
7777
}
7878

79+
createWebShareLink(vaultId: string): Either<VaultError, string> {
80+
try {
81+
const token = crypto.randomUUID();
82+
const expiresAt = Date.now() + 60 * 60 * 1000; // 1 hour
83+
84+
// Store token with vaultId for web sharing (using vaultId as chatId for web users)
85+
AccessTokenStore.store.set(token, {
86+
expiresAt,
87+
chatId: vaultId,
88+
vaultId,
89+
});
90+
91+
return right(token);
92+
} catch (error) {
93+
this.logger.error(`Error creating web share link: ${error}`);
94+
return left({
95+
type: VaultErrorType.INTERNAL_ERROR,
96+
message: 'Erro interno ao criar link de compartilhamento',
97+
});
98+
}
99+
}
100+
79101
getAccessTokenData(token: string) {
80102
const entry = AccessTokenStore.store.get(token);
81103
if (entry && entry.expiresAt > Date.now()) {
@@ -207,10 +229,13 @@ export class VaultWebService {
207229
description?: string;
208230
date?: { year: number; month: number };
209231
page: number;
232+
pageSize?: number;
210233
},
211234
): Promise<Either<VaultError, Paginated<TransactionDTO>>> {
212235
try {
213-
this.logger.log(`Getting transactions for vault: ${vaultId}`);
236+
this.logger.log(
237+
`Getting transactions for vault: ${vaultId} with page size: ${params.pageSize ?? 5}`,
238+
);
214239

215240
// Use VaultService directly
216241
const [error, transactions] = await this.vaultService.getTransactions({
@@ -222,6 +247,7 @@ export class VaultWebService {
222247
page: params.page || 1,
223248
categoryId: params.categoryId,
224249
description: params.description,
250+
pageSize: params.pageSize ?? 5,
225251
});
226252

227253
if (error !== null) {

src/vault/vault.controller.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class VaultController {
7474
description,
7575
date,
7676
page: pageNumber,
77+
pageSize: 5,
7778
},
7879
);
7980

@@ -266,6 +267,24 @@ export class VaultController {
266267
});
267268
}
268269

270+
@Post('create')
271+
async createVault(@Res({ passthrough: true }) response: Response) {
272+
const vault = await this.vaultService.createVault();
273+
274+
// Set HTTP-only cookie with the vault access token for automatic login
275+
response.cookie('vault_access_token', vault.token, {
276+
httpOnly: true,
277+
secure: process.env.NODE_ENV === 'production',
278+
sameSite: 'lax',
279+
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
280+
});
281+
282+
return {
283+
vaultId: vault.id,
284+
message: 'Carteira criada com sucesso! Você foi automaticamente logado.',
285+
};
286+
}
287+
269288
@UseGuards(VaultAccessTokenGuard)
270289
@Get('me')
271290
getMe(@VaultSession() vaultId: string) {
@@ -274,6 +293,21 @@ export class VaultController {
274293
};
275294
}
276295

296+
@UseGuards(VaultAccessTokenGuard)
297+
@Post('share-link')
298+
createShareLink(@VaultSession() vaultId: string) {
299+
const [error, token] = this.vaultAuthService.createWebShareLink(vaultId);
300+
301+
if (error !== null) {
302+
this.handleError(error.type, error.message);
303+
}
304+
305+
return {
306+
token,
307+
message: 'Link de compartilhamento gerado com sucesso!',
308+
};
309+
}
310+
277311
private handleError(error: VaultErrorType, message: string): never {
278312
switch (error) {
279313
case VaultErrorType.UNAUTHORIZED:

src/vault/vault.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ export class VaultService {
200200
page?: number;
201201
pageSize?: number;
202202
}) {
203-
this.logger.log(`Getting transactions for vault: ${input.vaultId}`);
203+
this.logger.log(
204+
`Getting transactions for vault: ${input.vaultId} with page size: ${input.pageSize ?? 10}`,
205+
);
204206
const vault = await this.vaultRepository.findById(input.vaultId);
205207
if (!vault) {
206208
this.logger.warn(`Vault not found: ${input.vaultId}`);

0 commit comments

Comments
 (0)