Commit 33030498 authored by Wei Han's avatar Wei Han

survey done

parent 02bbb1d6
......@@ -108,6 +108,11 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)requestSurvey:(void(^)(BOOL success, NSDictionary * _Nullable response, NSError * _Nullable error))completion;
+ (void)requestSubmitSurveyWithFormId:(NSString *)formId
surveyAnswers:(NSDictionary<NSNumber *, NSDictionary *> *)answers
completion:(void(^)(BOOL success, NSDictionary * _Nullable response, NSError * _Nullable error))completion;
@end
NS_ASSUME_NONNULL_END
......@@ -2623,6 +2623,108 @@
[task resume];
}
+ (void)requestSubmitSurveyWithFormId:(NSString *)formId
surveyAnswers:(NSDictionary<NSNumber *, NSDictionary *> *)answers
completion:(void(^)(BOOL success,
NSDictionary * _Nullable response,
NSError * _Nullable error))completion {
NSURL *url = [APIConfig urlWithPath:@"/framework/owner/survey/submitSurvey"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 🧱 Multipart boundary
NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
// 🔧 Helper to append a form field
void (^appendFormField)(NSString *, NSString *) = ^(NSString *key, NSString *value) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", value ?: @""]
dataUsingEncoding:NSUTF8StringEncoding]];
};
appendFormField(@"data[os]", [APIConfig os]);
appendFormField(@"data[drawing_plan_id]", [APIConfig drawingPlanId]);
appendFormField(@"data[survey][form_id]", formId);
// Device / app info
NSString *infoJson = [APIConfig deviceInfoJson]; // or inline JSON if needed
appendFormField(@"data[information]", infoJson);
[answers enumerateKeysAndObjectsUsingBlock:^(NSNumber *questionId,
NSDictionary *answer,
BOOL *stop) {
NSString *valueKey =
[NSString stringWithFormat:@"data[survey][%@][value]", questionId];
NSString *commentKey =
[NSString stringWithFormat:@"data[survey][%@][comment]", questionId];
NSString *value = @"";
NSString *comment = @"";
if (answer[@"value"] != nil) {
value = [answer[@"value"] description];
}
if (answer[@"comment"] != nil) {
comment = [answer[@"comment"] description];
}
appendFormField(valueKey, value);
appendFormField(commentKey, comment);
}];
// 🔚 Close multipart body
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
request.HTTPBody = body;
NSLog(@"🌍 Submit Survey URL: %@", url.absoluteString);
NSLog(@"📦 Submit Survey Body:\n%@",
[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]);
NSURLSessionDataTask *task =
[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if (error) {
NSLog(@"❌ Submit survey network error: %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(NO, nil, error);
});
return;
}
NSError *jsonErr = nil;
NSDictionary *json =
[NSJSONSerialization JSONObjectWithData:data
options:0
error:&jsonErr];
NSLog(@"🧩 Submit Survey response: %@", json);
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(jsonErr == nil, json, jsonErr);
}
});
}];
[task resume];
}
#pragma mark - Helper Methods
+ (NSString *)multipartFormField:(NSString *)name value:(NSString *)value boundary:(NSString *)boundary {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment