Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 9711035

Browse files
author
Ismaila Abdoulahi
committed
Handle participant picture upload
1 parent c096442 commit 9711035

File tree

7 files changed

+26
-7
lines changed

7 files changed

+26
-7
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"dotenv": "^8.0.0",
3333
"graphql": "^14.4.2",
3434
"graphql-tools": "^4.0.5",
35+
"graphql-upload": "^8.0.7",
3536
"mongoose": "^5.6.5",
3637
"reflect-metadata": "^0.1.12",
3738
"rimraf": "^2.6.2",

schema.gql

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Mutation {
1010
input NewParticipantInput {
1111
firstName: String!
1212
lastName: String!
13-
picture: String
13+
picture: Upload
1414

1515
"""Participant status (e.g. Student, Coach, etc.). Default = Student"""
1616
status: String
@@ -39,3 +39,6 @@ input SocialMediaInput {
3939
url: String!
4040
platform: String
4141
}
42+
43+
"""The `Upload` scalar type represents a file upload."""
44+
scalar Upload

src/file/file.module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ import { ConfigModule } from '../config/config.module';
55
@Module({
66
imports: [ConfigModule],
77
providers: [FileService],
8+
exports: [FileService],
89
})
910
export class FileModule {}

src/file/file.service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export class FileService {
1515

1616
/**
1717
* Stores file by passing it to the cloud storage
18-
* @param {Stream} file a file stream
18+
* @param file a file stream
1919
* @param {string} tags tags to give to the file
2020
* @param {string} folder folder in which to store the file
2121
* @param {object} cloudinary cloudinary functions
2222
* @param {boolean} generated flag that tells whether the file has been generated by the backend or not
2323
* @returns {Promise} Promise that represents the file
2424
*/
25-
store(file: Stream, folder: string, tags: string): Promise<any> {
25+
store(file, folder: string, tags?: string): Promise<any> {
2626
const country = this.configService.get('COUNTRY');
2727
const edition = this.configService.get('EDITION');
2828
return new Promise((resolve, reject) => {
@@ -35,7 +35,7 @@ export class FileService {
3535
return reject(err);
3636
},
3737
);
38-
file.pipe(uploadStream);
38+
file.createReadStream().pipe(uploadStream);
3939
});
4040
}
4141
}

src/participant/inputs/new.particpant.input.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { InputType, Field } from 'type-graphql';
2+
import { GraphQLUpload } from 'graphql-upload';
23
import { SocialMediaInput } from './social.media.input';
34

45
@InputType()
@@ -9,8 +10,8 @@ export class NewParticipantInput {
910
@Field()
1011
lastName: string;
1112

12-
@Field({ nullable: true })
13-
picture?: string;
13+
@Field(type => GraphQLUpload, { nullable: true })
14+
picture?: any;
1415

1516
@Field({
1617
nullable: true,

src/participant/participant.module.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import { ParticipantService } from './participant.service';
33
import { ParticipantResolver } from './participant.resolver';
44
import { MongooseModule } from '@nestjs/mongoose';
55
import { ParticipantSchema } from './schemas/participant.schema';
6+
import { FileModule } from '../file/file.module';
67

78
@Module({
8-
imports: [MongooseModule.forFeature([{ name: 'Participant', schema: ParticipantSchema }])],
9+
imports: [
10+
MongooseModule.forFeature([
11+
{ name: 'Participant', schema: ParticipantSchema },
12+
]),
13+
FileModule,
14+
],
915
providers: [ParticipantResolver, ParticipantService],
1016
})
1117
export class StudentModule {}

src/participant/participant.service.ts

+7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ import { Injectable } from '@nestjs/common';
22
import { IParticipant } from './interfaces/participant.interface';
33
import { InjectModel } from '@nestjs/mongoose';
44
import { Model } from 'mongoose';
5+
import { FileService } from '../file/file.service';
56

67
@Injectable()
78
export class ParticipantService {
89
constructor(
910
@InjectModel('Participant')
1011
private readonly participantModel: Model<IParticipant>,
12+
private readonly fileService: FileService,
1113
) {}
1214

1315
async create(input: any): Promise<IParticipant> {
16+
const { picture } = input;
17+
if (picture) {
18+
const file = await this.fileService.store(await picture, 'participants');
19+
input.picture = file.secure_url;
20+
}
1421
return this.participantModel.create(input);
1522
}
1623

0 commit comments

Comments
 (0)