Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new endpoint, forgot pwd fix #671

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/src/controllers/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { permissionActions } from '../enums/permissions/permission-actions-enum'
import { PermissionAction } from '../decorators/permission-action.decorator';
import { ApplicationCsvExporterService } from '../services/application-csv-export.service';
import { ApplicationCsvQueryParams } from '../dtos/applications/application-csv-query-params.dto';
import { MostRecentApplicationQueryParams } from '../dtos/applications/most-recent-application-query-params.dto';

@Controller('applications')
@ApiTags('applications')
Expand Down Expand Up @@ -76,6 +77,18 @@ export class ApplicationController {
return await this.applicationService.list(queryParams);
}

@Get(`mostRecentlyCreated`)
@ApiOperation({
summary: 'Get the most recent application submitted by the user',
operationId: 'mostRecentlyCreated',
})
@ApiOkResponse({ type: Application })
async mostRecentlyCreated(
@Query() queryParams: MostRecentApplicationQueryParams,
): Promise<Application> {
return await this.applicationService.mostRecentlyCreated(queryParams);
}

@Get(`csv`)
@ApiOperation({
summary: 'Get applications as csv',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Expose } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { ValidationsGroupsEnum } from '../../enums/shared/validation-groups-enum';
export class MostRecentApplicationQueryParams {
@Expose()
@ApiProperty({
type: String,
example: 'userId',
})
@IsString({ groups: [ValidationsGroupsEnum.default] })
userId: string;
}
24 changes: 24 additions & 0 deletions api/src/services/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Listing from '../dtos/listings/listing.dto';
import { User } from '../dtos/users/user.dto';
import { permissionActions } from '../enums/permissions/permission-actions-enum';
import { GeocodingService } from './geocoding.service';
import { MostRecentApplicationQueryParams } from '../dtos/applications/most-recent-application-query-params.dto';

export const view: Partial<
Record<ApplicationViews, Prisma.ApplicationsInclude>
Expand Down Expand Up @@ -120,6 +121,29 @@ export class ApplicationService {
};
}

/*
this will the most recent application the user has submitted
*/
async mostRecentlyCreated(
params: MostRecentApplicationQueryParams,
): Promise<Application> {
const rawApplication = await this.prisma.applications.findFirst({
select: {
id: true,
},
orderBy: { createdAt: 'desc' },
YazeedLoonat marked this conversation as resolved.
Show resolved Hide resolved
where: {
userId: params.userId,
},
});

if (!rawApplication) {
return null;
}

return await this.findOne(rawApplication.id);
}

/*
this builds the where clause for list()
*/
Expand Down
2 changes: 2 additions & 0 deletions api/src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ export class AuthService {
passwordHash: await passwordToHash(dto.password),
passwordUpdatedAt: new Date(),
resetToken: null,
confirmedAt: user.confirmedAt || new Date(),
confirmationToken: null,
},
where: {
id: user.id,
Expand Down
43 changes: 43 additions & 0 deletions api/test/unit/services/app.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Logger } from '@nestjs/common';
import { SchedulerRegistry } from '@nestjs/schedule';
import { randomUUID } from 'crypto';
import { AppService } from '../../../src/services/app.service';
import { PrismaService } from '../../../src/services/prisma.service';

Expand All @@ -23,4 +24,46 @@ describe('Testing app service', () => {
});
expect(prisma.$queryRaw).toHaveBeenCalled();
});

it('should create new cronjob entry if none is present', async () => {
prisma.cronJob.findFirst = jest.fn().mockResolvedValue(null);
prisma.cronJob.create = jest.fn().mockResolvedValue(true);

await service.markCronJobAsStarted();

expect(prisma.cronJob.findFirst).toHaveBeenCalledWith({
where: {
name: 'TEMP_FILE_CLEAR_CRON_JOB',
},
});
expect(prisma.cronJob.create).toHaveBeenCalledWith({
data: {
lastRunDate: expect.anything(),
name: 'TEMP_FILE_CLEAR_CRON_JOB',
},
});
});

it('should update cronjob entry if one is present', async () => {
prisma.cronJob.findFirst = jest
.fn()
.mockResolvedValue({ id: randomUUID() });
prisma.cronJob.update = jest.fn().mockResolvedValue(true);

await service.markCronJobAsStarted();

expect(prisma.cronJob.findFirst).toHaveBeenCalledWith({
where: {
name: 'TEMP_FILE_CLEAR_CRON_JOB',
},
});
expect(prisma.cronJob.update).toHaveBeenCalledWith({
data: {
lastRunDate: expect.anything(),
},
where: {
id: expect.anything(),
},
});
});
});
53 changes: 53 additions & 0 deletions api/test/unit/services/application.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1588,4 +1588,57 @@ describe('Testing application service', () => {

expect(canOrThrowMock).not.toHaveBeenCalled();
});

it('should get most recent application for a user', async () => {
const date = new Date();
const mockedValue = mockApplication(3, date);
prisma.applications.findUnique = jest.fn().mockResolvedValue(mockedValue);
prisma.applications.findFirst = jest
.fn()
.mockResolvedValue({ id: mockedValue.id });

expect(await service.mostRecentlyCreated({ userId: 'example Id' })).toEqual(
mockedValue,
);
expect(prisma.applications.findFirst).toHaveBeenCalledWith({
select: {
id: true,
},
orderBy: { createdAt: 'desc' },
where: {
userId: 'example Id',
},
});
expect(prisma.applications.findUnique).toHaveBeenCalledWith({
where: {
id: mockedValue.id,
},
include: {
userAccounts: true,
applicant: {
include: {
applicantAddress: true,
applicantWorkAddress: true,
},
},
applicationsMailingAddress: true,
applicationsAlternateAddress: true,
alternateContact: {
include: {
address: true,
},
},
accessibility: true,
demographics: true,
householdMember: {
include: {
householdMemberAddress: true,
householdMemberWorkAddress: true,
},
},
listings: true,
preferredUnitTypes: true,
},
});
});
});
2 changes: 2 additions & 0 deletions api/test/unit/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ describe('Testing auth service', () => {
passwordHash: expect.anything(),
passwordUpdatedAt: expect.anything(),
resetToken: null,
confirmedAt: expect.anything(),
confirmationToken: null,
},
where: {
id,
Expand Down
21 changes: 21 additions & 0 deletions shared-helpers/src/types/backend-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,27 @@ export class ApplicationsService {
axios(configs, resolve, reject)
})
}
/**
* Get the most recent application submitted by the user
*/
mostRecentlyCreated(
params: {
/** */
userId: string
} = {} as any,
options: IRequestOptions = {}
): Promise<Application> {
return new Promise((resolve, reject) => {
let url = basePath + "/applications/mostRecentlyCreated"

const configs: IRequestConfig = getConfigs("get", "application/json", url, options)
configs.params = { userId: params["userId"] }

/** 适配ios13,get请求不允许带body */

axios(configs, resolve, reject)
})
}
/**
* Get applications as csv
*/
Expand Down
9 changes: 3 additions & 6 deletions sites/public/src/pages/applications/start/autofill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,12 @@ export default () => {
if (!previousApplication && initialStateLoaded) {
if (profile) {
void applicationsService
.list({
.mostRecentlyCreated({
userId: profile.id,
orderBy: ApplicationOrderByKeys.createdAt,
order: OrderByEnum.desc,
limit: 1,
})
.then((res) => {
if (res && res?.items?.length) {
setPreviousApplication(new AutofillCleaner(res.items[0]).clean())
if (res) {
setPreviousApplication(new AutofillCleaner(res).clean())
} else {
onSubmit()
}
Expand Down
Loading